const users = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
function countOnline(usersObj) {
// Only change code below this line
let result = 0;
for (let user in usersobj) {
if (usersobj[user].online === true) {
result++;
}
}
return result;
// Only 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:97.0) Gecko/20100101 Firefox/97.0
Challenge: Iterate Through the Keys of an Object with a for…in Statement
How it came about. I applied it while I was reading the tread on fcc forum, while I was trying to solve the challenge. but didn’t know what the code actually did on the challenge
To understand what the code is doing we first have to break down the problem in plain english.
Our goal is to find out how many users are currently online at the moment.
If you look at the list of users here you can see we only have one user online which is Jeff.
Result represents the number of people currently online.
Before you loop through the object it needs to be initialized to 0 since we haven’t checked yet if anyone is online.