I'm trying to return the number of users that are online as determined by this JS object. However, my counter for users that are currently online never gets incremented.
I use the for-in loop to iterate through the JS object, and then I increment the 'usersOnline' variable if the specific user's 'online' property is set to true.
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) {
let usersOnline = 0;
for (let user in obj) {
if (user.online === true)
usersOnline++;
}
return usersOnline;
}
console.log(countOnline(users));
usersOnline should be incremented twice so that it is equal to 2. But it stays set at 0. This problem is part of a coding challenge on freeCodeCamp.com I'm more interested in why this specific code won't work versus how to use JS objects in general.