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

Tell us what’s happening:
Hi everyone, having trouble with this exercise – please help! My code so far:

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 obj) if (obj[user][online] == true) {
return count++;
}
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 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

is online supposed to be a variable? Where is it defined?

1 Like

online is supposed to be a key in the Alan object, which is nested within the users object – so yeah, then it shouldn’t be defined because it’s not a variable…right?

I mean, users is a variable which is defined. anyway you can see that I’m confused lol

Review the difference between bracket notation and dot notation when looking up object properties.

1 Like