I keep getting “users is not defined” when i console.log this. It’s exactly the solution that was provided.
Your code so far
function countOnline(usersObj) {
// Only change code below this line
let result = 0;
for (let user in obj) {
if (obj[user].online === true) {
result++;
}
}
return result;
}
console.log(countOnline(users))
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13020.82.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.112 Safari/537.36.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
Yeah i guess. The solution has that parameter but it’s above the “only change code beneath this line” line so i didn’t think it was part of the actual solution. I’m sure that will work. Just a deviation from the way everything has been up to now. Thanks.
Copy and pasting the code like it is in the first post is best.
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.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
let 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 obj) {
if (obj[user].online === true) {
result++;
}
}
return result;
}
console.log(countOnline(users));
Exactly. and that’s basically what is going on with your function.
function countOnline(usersObj) {
// Only change code below this line
let result = 0;
for (let user in obj) {
if (obj[user].online === true) {
result++;
}
}
return result;
}
Your function knows about usersObj but you are using obj.
Nice job! I know it can be frustrating when everyone on here gives you hints without actual code, but learning to walk through your program and notice things that are out of place like an unmatched bracket or an undefined variable is a skill that everyone needs to master. And luckily once you start programming in an IDE, there are tools like ESLint that do a lot of that work for us. But for now, in the freeCodeCamp editor, you need to be vigilant about variable names yourself.
@JeremyLT Thanks for wrapping up my train of thought there