Tell us what’s happening:
I cannot figure this out to save my life!! I’ve been stuck on it for nearly the entire day and I have read every post I can find, as well as tried the hint that is provided and I continue to get error codes. PLLEASE SOMEONE HELP ME!!
Your code so far
const users = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
function countOnline(allUsers) {
// Only change code below this line
let result = 0;
for (let user in usersObj) {
if (usersObj[Alan].online === true) {
result++;
}
// Only change code above this line
}
console.log(countOnline(users));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.58
Challenge: Basic Data Structures - Iterate Through the Keys of an Object with a for…in Statement
Link to the challenge:
Do you only care about Alan
? Or do you care about all users who are set to true
?
Also, your function should probably return the number of users who meet the criteria.
P.S. It looks like you are missing a curly closing brace as well. You should be seeing a syntax error in the console pane.
Jeff is the only user that is set to true. Sarah and Alan are both set to false. And I tried to include them all, in order, and still receive an error.
Yes, for the users
object defined above the function. But what if the tests pass in a different object that doesn’t have those names? Your function should be able to handle any object that is formatted like the example. You shouldn’t need to know the names of the people in the object in order to implement the solution.
What is this doing?
im so confused

i just went back and reviewed the last section.
it keeps telling me that usersObj is not defined…
Ahh, I missed that one. Look at the parameter list for your function:
function countOnline(allUsers) {
This means the object passed into your function will have the name allUsers
when you refer to it inside of the function.
Now look at your for...in
loop:
for (let user in usersObj) {
Does usersObj
exist inside of your function? What should you use instead?
i’ve tried it with both countOnline and allUsers and i am still getting error messages. I’m starting to get suuper frustrated. Thank you for trying to help! I am going to take a short break and revisit later.
In general, you should only ever use the variables listed as function arguments.
Should I have more than one number listed for
function countOnline(allUsers) {
let result = **??**
for (let allUsers in countOnline) {
if (allUsers[users].online === true)
result++;
}
}
also I am getting this error message? |
|
—> SyntaxError: unknown: Unexpected token (22:32) |
20 | // Only change code above this line
21 |
22 | console.log(countOnline(users));
|
I don’t think this does what you think.
const refrigerator = {
'milk': 1,
'eggs': 12,
};
for (const food in refrigerator) {
console.log(food, refrigerator[food]);
}
The syntax is for (const myCurrentKey in someObject)
The allUsers
is your object. What is inside of the allUsers
object?
allUsers[users]?
i am having way to difficult of a time with this right now. lol 
This question is kind of sneaky in asking for the truthy value of the online status of each person. Here is why I write that it’s sneaky. If you compare the sample code to the code in your problem you’ll notice that there is a slight difference.
Here is the refrigerator object
const refrigerator = {
'milk': 1,
'eggs': 12,
};
Here is the users object from your problem:
const users = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
The difference is that the truthy/falsy online
value/method that the problem depends on is inside or embedded in the second level of the users
object whereas the refrigerator
object only the one level of 'milk'
and 'eggs'
. So iterating once using the for...in
statement will only get you the first level of the users
object which is only the so called first level of the users object. So if you iterate once to get to the first level that has each user like Alan
or Jeff
. But what do I need to do to get to the second level to get the truthy or falsy online
value?
1 Like
yeah, thats why I cant figure it out. Im not sure how to specify that second level in the objects. by using a variable?
idk 
Users might be a good variable name, but you need to declare it like ‘food’ is
let result = 1;
for (let countOnline in allUser) {
if (allUser[false].online === true) {
result++;
}
}
return result;
this passes only one of the tests
1 Like
What is this doing? Explain in words what this means.
Also, be very careful about spelling. If the object passed into the function is named allUsers
then you need to use that name in the function body (i.e. it needs to be spelled the same way).
Try calling the for...in
loop twice. It’s like a big box(objectOne
) containing smaller boxes(objectA
, and objectB
. the first for...in
opens the outside big box. The second for...in
loop opens each of the smaller boxes.
You can cut and paste this code to run it and see how it works
const objectOne = {
objectA: {
trueOrFalse: true
},
objectB: {
trueOrFalse: false
}
}
function getTheValue() {
for(const variableOne in objectOne) {
for(const variableTwo in objectOne[variableOne]) {
console.log(objectOne[variableOne][variableTwo])
}
}
}
getTheValue();
@malloryoncesaid, I just want to make clear that you are on the right track with this approach, you can just use a single for
loop to solve this, but you aren’t quite using the variables correctly in the if
statement.
Also, the name of the object is allUsers
(with an s
at the end).
I keep asking you to explain what the first line of the for
loop means because you need to understand what those variables represent in order to write the if
statement correctly.
You are close. Don’t give up. Please continue to ask questions if something isn’t clear.
P.S. You probably don’t want to start result
at 1
because that would mean that your function would always return at least 1
, but what if there aren’t any users online?
I suggest you change userObj to the function param.
And the result variable should be initiated to zero
Mod Edit: SOLUTION REMOVED