Iterating objects

Tell us what’s happening:

where am i going wrong.

We’ve defined a function countOnline which accepts one argument (a users object). Use a for…in statement within this function to loop through the users object passed into the function and return the number of users whose online property is set to true . An example of a users object which could be passed to countOnline is shown below. Each user will have an online property with either a true or false value.

Your code so far
function countOnline(usersObj) {
// Only change code below this line
let result = 0;
for (let user in obj) {
if (obj[user].online === true) {
result++;
}
}
return result;
// Only change code above this line
}


let users ={
Alan: {
  age: 27,
  online: false
},
}
function countOnline(usersObj) {
// Only change code below this line
let result = 0;
for (let user in obj) {
if (obj[user].online === true) {
  result++;
}
}
return result;
// 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/84.0.4147.125 Safari/537.36 Edg/84.0.522.59.

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

Link to the challenge:

where does obj comes from?

there is nothing named obj in your code

1 Like

that line really confuses me like how do i fix this code and what does that line mean. please help

Hi, I still can’t figure it out could you please show me the correct code

Your function accepts as an argument usersObj

But then you try to use obj which is not defined anywhere

So what you want to use inside the function is usersObj not obj.

:wink: