Stuck in key and values in javascript

Tell us what’s happening:
I tried to solve this question but could not get it
please help me

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
  for(let user in users){
    return users
    if(users[user].online == true){
        return users[user]
    }  
  }

  // change code above this line
}

console.log(countOnline(users));

Your browser information:

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/

This exercise enforces bad habits.
You should almost never use for… in.

By using for…in you’re also checking prototype chain properties as well.
Object.entries is the way to go.

But., I’m not going to post anymore since all my suggestions appear to piss off admins. Good luck FCC.

1 Like

Yes
I can use with that but they want to use for…in and i cant solve this problem for half and hour

  1. Loop starts.
  2. You return the object you’re looping over (users).
  3. Loop immediately stops, because:
  4. Function exits.

I’m unsure why you’re doing 2, as that immediately stops you from doing anything else.

Second thing is that [if you get rid of that line], once again, return is used, so when you reach the first user who is online, the information about them is returned and the function exits.

You are supposed to be counting how many users are online, not returning information about them, so this gives you a hint that you need to keep a running count and return that once you’ve checked all the users.