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