Basic Data Structures - Iterate Through the Keys of an Object with a for...in Statement

Tell us what’s happening:
I’m getting stuck on this challenge. I’ve read through a few of the topics on this forum but can’t find a straight forward answer to the problem. The solution proposed also doesn’t seem to work. I’m going around in circles. Thanks in advance for your responses. Me and Javascript don’t get on very well.

Your code so far

const users = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

function countOnline(usersObj) {
  // Only change code below this line
let countOnline = 0;
for (let users in usersObj){
  if (users.online === true) {countOnline +=1;}
};
return countOnline;
  // Only 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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

Challenge: Basic Data Structures - Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

When using in what you get isn’t the user object, but just the key. So in your loop, users would just be the users name, a String, not the whole object.

I like to use console.log() to troubleshoot issues, so if you put console.log(users) inside your loop you will see what users contains.

1 Like

Thank you. Very helpful.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.