What is the issue with my solution?

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 usr in obj)
{
  if(usr.online == true)
       count++;
}
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/75.0.3770.142 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

That’s because for ... in loop iterates over keys of an object, not the values they reference. The keys are just strings like “Alan” and “Jeff”. But if you have the key name, you can access its value. You need to make a slight alteration inside your loop body.

Try adding console.log(usr) inside your loop - I think that will answer your question

1 Like

I tried the exact same approach when I came up on this challenge!

I am trying , but haven’t reached to the final result

no this is not the answer.
the program needs to return count value in case online==true

if you return count value immediately then you have max value of 1, you need to return the number of online users