Tell us what’s happening:
The exercise as asking to count the number of users with an online property of true, but I’m having trouble accessing this property and I’m not sure where I’m going wrong. The user object looks like:
Alan: {
online: false
}
So I tried
user['online']
to access the online property, but it isn’t working. When I log the output of user[online] to
console, it’s printing out undefined, which confuses me. I tried capitalizing the O as well as iterating through the properties of the user object to see if that would help, but didn’t have any luck. Any information would be helpful. Thanks so much.
Your code so far
function countOnline(usersObj) {
// change code below this line
let online = 0;
for (let user in usersObj) {
console.log(user);
console.log(user['online');
for (let prop in user) {
console.log(prop);
}
if (user['online']) {
online++;
}
}
return online;
// change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement