Iteratation through the keys of an object

Tell us what’s happening:
Hello, I have written code with the user having their respective online property value but I am not getting the appropriate return when the objects are passed through.

The function countOnline should return 2 when the object
{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }
is passed to it

Your code so far


function countOnline(usersObj) {
 // Only change code below this line
let user = {
Alan: {
 online: true
},
Jeff: {
 online: false
},
Sarah: {
 online: true
},


};
{
 let online = 0;
 for(let user in usersObj) {
   if(user.online === true) 
    online++;
   
   
 }
return online;
}

 // Only change code above this line
}

Your browser information:

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

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

Link to the challenge:

user is a string, it doesn’t have an online property

1 Like

In the for/in statement, as in this exercise, the user is the key which is the string Alan (or other names). We need to use userObj[user] to get the value {online: true} of a key.

I think the user object is already be declared on the left (or somewhere) that we don’t need to declare it again, especially not in the function. Because the user object is the argument which passes in the countOnline function.

2 Likes