Tell us what’s happening:
Can’t seem to get the help from the get help tab.
Your code so far
function countOnline(obj) {
// change code below this line
let result = 0;
for (let user in obj) {
if (obj[user].online === true) {
result++;
}
}
return result;
// change code above this line
}
console.log(countOnline(users));
function countOnline(obj) {
// change code below this line
let counter = 0;
for (let user in obj) {
if(user.online === true){
counter++;
}
}
return counter;
// change code above this line
}
console.log(countOnline(users));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement
Hi @DevinCassidy,
Here is a hint. Log out to the console the typeof user
within your for loop. Then with that knowledge, think about what user.online is actually referencing vs what you want it to reference.
Maybe spend some time with the MDN documentation for for…in.
Spend some time and play with it and if you’re still having trouble come back and post what you tried and what your thought process is and I’ll try to guide you to the solution.
Also, for your console.log message on the last line of your sample code, be sure to define users to an object, like that described in the lesson text, so you don’t get a ReferenceError.
let users = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
1 Like
this is what I have so far
function countOnline(usersObj) {
// Only change code below this line
let result = 0;
for (let user in obj) {
if (obj[users][online] === true) {
result++;
}
}
return result;
// Only change code above this line
}
Closer! You have a couple of issues here you need to work out though.
- Pay close attention to the name of the parameter that is being passed in. Are you using that in the right spots?
- Look at your variable name used in the
for...in
as well and how it’s being used in the if statement.
- The usage of
online
isn’t quite right, but you’re close. With the bracket notation it thinks online
is a variable, but you want to reference it as a property.
Think about those and try to clean it up a bit. You’re on track, almost there, just push through this a little more.
1 Like