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

Tell us what’s happening:

Your code so far


let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};
 let usersOnline=0;
function countOnline(obj) {
  // change code below this line
 
for(let user in users)
{
  if(obj[user].online==true)
  {
    usersOnline++;
  }
  
}
  // change code above this line
return usersOnline;
}

console.log(countOnline(users));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/-iterate-through-the-keys-of-an-object-with-a-for---in-statement

Hi
the let usersOnline=0; statement need to be inside the core function to let all tests pass.

1 Like