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

Tell us what’s happening:

It’s the usual ‘I expect this to work, but it is not accepted, can someone please tell me why’ kind of question.

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 r = 0;
  for(let user in obj) {
    if(user.online) {
      r = r + 1;
    }
  }
  return r
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15.

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

The get-a-hint link results in a. 404

Hi,

You don’t access the user object, only its’ key :slight_smile:

1 Like

this is not how you access an object property, user is a string, so it doesnt have a property. user instead is the property name

1 Like

Aha, so I have to use this as a key into the object; obj[user].online …