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

what is the mistake ,running tests error

const users = { Alan: { online: true }, Jeff: { online: true }, Sarah: { online: false } }

function countOnline(usersObj) {
// Only change code below this line
let count=0;
for (let usersObj in users) {
if(users[usersObj][‘online’]) {
count++;
}
}
return count;
// Only change code above this line
}
console.log(countOnline(users));

Please post a link to the challenge website.

The syntax of your loop is wrong.

let object = {1: "a", 2: "b"};
for (let key in object) {
  console.log(object[key]); 
}
output:
a
b

You also try to declare usersObj as the variable in your loop, but it’s already the object you pass into the function. Instead you use the global object users

Nice explanation, I typed about the same a minute later lol.

The more different explanations, the higher the chance that it will be understood - no need to delete it :wink:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.