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

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

#1

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


#2

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


#3

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?


#4

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


#5

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