hello there,sorry for getting back after long.
my logic is , i am simply iterating an array and checking if object as online property , if it turns out to be true count gets increase else it returns zero.
@alkapwn3d, I really doubt that for ... of loop would want for work with object. For ... in is correct way.
@omkarkothavale88, when you doubt about something I would suggest using your browser’s console to check what’s going on:
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
for (let users in users) {
console.log(users);
}
// Reference error: you cannot do (let A in A)
for (let user in users) {
console.log(user);
}
// We get keys of the object (Alan, Jeff...) not values, so user === 'online' won’t work.
// Let’s try to get values:
for (let user in users) {
console.log(users[user]);
}
// Now you get the access to the values and can check if users[user].online
Now to the other stuff,
return will break your loop and immediately return the outcome. (Always 0 in your case - your app has no users online. Sad!)
You don’t have to compare truthy condition to true, the expression itself is enough: