Need Help.... Iterate Through the Keys of an Object 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
  }
};

function countOnline(obj) {
  // change code below this line
let count=0
for (let user in users){
  if (users[user]['online'] === true) {
   count++ 
  } else
  return count;
}
  // change code above this line
}

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/74.0.3729.169 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

Need help don’t know where is the mistake… Thanks to everybody will let me understand…

Return the count after the loop finishes. What you’re currently doing is, if a user is online, you’re adding to the count. But as soon as you reach one that isn’t online, you return that count, exiting the loop and the function. You don’t need an else clause

you are a genius… Thank a lot!

1 Like