Could Please check my code, really I am not sure, Been honest almost guessing some parts.
my code past the test. Why?
I read a lot of: Object.entries, values, keys.
examples in MDN, 3school and other pages.
Your code so far
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) {
// change code below this line
let userOnline = 4;
for(let user in obj){
if(obj[user].userOnline = true){
userOnline++;
return userOnline;
}
};
// change code above this line
}
console.log(countOnline(users));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.
There are many errors here, I’ll just point them out but really I suggest you go back and review the lessons prior to this challenge well before you go any further
let userOnline = 4; // why are you setting this ?
for(let user in obj){ // what is obj ??
if(obj[user].userOnline = true){ //what is the difference between a comparison and assignment operator ??
userOnline++;
return userOnline; // you are returning out of the for loop why ?
}
};
I really emphasize that you go back to the previous lessons like these and come back with questions
You have like 7 different challenges in a post, none of them correctly formatted, it is a bit difficult to be able to help you with this
To format your code, select a block of code in the message editor and then use the “Preformatted text” </> button, so that it will be surrounded by backticks
Let’s see this last one:
You can’t know how many users are online, so you should initialise userOnline to 0.
You are also using if(obj[user].userOnline = true) as a condition, and as you are using an assignment operator and not a comparison this will never be false
Then you have a return statement inside s loop, that means that the first iteration in which the return is executed the function stops and a value is returned from the function, effectively counting only the first online user.
You have a few issues to solve here
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 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.