R
Reactiflux

JS decreasing

JS decreasing

EEnfield-Li12/29/2021
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)
SS3BAS12/29/2021
That line doesn't do anything indeed If you log the obj afterwards, you'll see the amounts haven't changed
EEnfield-Li12/29/2021
I accidentally put a comment there... but yeah, I log out the obj in loop, the object stay the frogchamp
SS3BAS12/29/2021
Even if it's not a comment It would still be the same yeah
EEnfield-Li12/29/2021
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!😺
SS3BAS12/29/2021
Exactly! meowthumbsup no problem :)

Looking for more? Join the community!

R
Reactiflux

JS decreasing

Join Server