Enfield-Li
Enfield-Li3y ago

JS decreasing

I have a js question, just can't figure out 😵‍💫 , can someone help me understand the code, why can we decrease the object value right away? Code here: function checkItem(arr) { let obj = { a: 1, b: 1, c: 1 }; for (let i = 0; i < arr.length; i++) { let char = arr[i]; if (!obj[char]) { return false; } else { // obj[char] - 1; // ⬅ How does it work? } } return true; } let arr = ["a", "b", "c"]; console.log(checkItem(arr)); (PS: the function is for checking if the value in arr existing in an object with the right amount)
5 Replies
S3BAS
S3BAS3y ago
That line doesn't do anything indeed If you log the obj afterwards, you'll see the amounts haven't changed
Enfield-Li
Enfield-LiOP3y ago
I accidentally put a comment there... but yeah, I log out the obj in loop, the object stay the frogchamp
S3BAS
S3BAS3y ago
Even if it's not a comment It would still be the same yeah
Enfield-Li
Enfield-LiOP3y ago
Yeah, i think the case sovled, if we were to decrese the object value, I have to do this: obj[char] = obj[char] - 1; Thank you for your help! truly appreciated!😺
S3BAS
S3BAS3y ago
Exactly! meowthumbsup no problem :)

Did you find this page helpful?