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

Tell us what’s happening:

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
for(let users in obj){
 if(users.hasOwnProperty(true)){
   return users;
 }
}
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

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/

What is your question?

Here was my solution but it is not working .

if(users.hasOwnProperty(true)){
   return users;
 }

This doesn’t do what’s being asked in the challenge.

The challenge wants you to filter the result,
you can do it in many ways.

Think about it.

Here you are checking to see if each object has a property called true. It will always be false because there are no properties called true. You are only checking if online property is true. But using bracket notation or dot notation will give you access to online values in each object. Then compare if they are true. You also don’t want to return users right away. I would create a counter variable and increment it if your if statements are true then return the counter at the end of the function.

Let me try again.I’m now confused.

Another hint: If an object key is a variable, bracket notation must be used. If it’s a constant, either dot or bracket notation can be used.

1 Like