Iterate Through the Keys of an Object with a for...in Statement(why not working)

Iterate Through the Keys of an Object with a for...in Statement(why not working)
0

#1

Can anybody help me with this code

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) {
       count ++;
     }
   }
   return count;
  // 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) Chrome/69.0.3497.23 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

You are trying to access the property of “user” instead of accessing the property “Alan, Jeff…etc” and then getting their online info. You need to dynamically check for this. Console log what “user” actually is and u should see the answer


#3

thank you so much…Eddie. It helped me:smiley: