Confused about Iterate Through the Keys of an Object with a for...in Statement challenge

Tell us what’s happening:
This just doesn’t make sense. usersObj.user.online == true only works if I use bracket notation for user, and I don’t understand why. It just saysTypeError: Cannot read property ‘online’ of undefined if I don’t.

Your code so far


function countOnline(usersObj) {
 // Only change code below this line
var i = 0;
for (let user in usersObj){
 if (usersObj.user.online == true) {
   i = i + 1;
   console.log(i);
 }
}
return i;
 // Only change code above this line
}
countOnline({ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } })

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

user is a variable. You can’t use dot notation with a variable. You need to use bracket notation.

1 Like