Of course it does not work. What is obj ? First you declare you users object like ‘users’, and you are looping obj. Change that. Second what is user in the loop. DId you log it?
Try that:
let count = 0;
for(let user in users) {
if(users[user].online === true) {
count += 1;
}
}
This why i had that ‘obj’. My problem was that I forgot I have to reference obj in the contidion.
Thanks
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 count = 0;
for(let user in obj) {
if(obj[user].online === true) {
count += 1;
}
}
return count;
}
console.log(countOnline(users));
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.