Accessing JS object data incorrectly? [SOLVED]

Tell us what’s happening:
In my mind this should work. But I’m obviously doing something wrong. usersOnline never gets incremented. I can’t seem to find a good answer on StackOverflow and the get a hint page for this challenge is down. Any advice?

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 usersOnline = 0;

  for (let user in obj) {
    if (user.online === true)
      usersOnline++;
  }

  return usersOnline;
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/74.0.3729.169 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

Try logging out the type of user in the for…in loop.

console.log(typeof user);

Have a look at the for…in docs to see how to access the object properties

I figured it out. I just needed to go a level deeper when accessing the object’s values.

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 usersOnline = 0;

  for (let user in obj) {
    if (obj[user]["online"] === true)
      usersOnline++;
  }

  return usersOnline;
  // change code above this line
}

console.log(countOnline(users));