# Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement*

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
What is the reason why this does not work?

``````function countOnline(usersObj) {
// Only change code below this line

for(let user in usersObj ){
let count=0;
if (user==online){
let newCount=count ++;
}return newCount;
}

// Only change code above this line

}
``````

It seems you may not have thought through the algorithm (logic steps) before writing the code. Can you explain your algorithm first? Your code has several issues that indicate your understanding of a working algorithm is missing.

Once you explain what you believe the correct algorithm is, then we can help guide you to what code you might want to write.

1 Like

Thanks for your answer. I want to go trough my object, when I am into it (let count=0;) I started to count from 0. When I get in the loop, I write if the user is ist equal to online, then, if that is true, I sum 1 to newCount. The cycle repeat three times more and I should get in newAccount, the people who is online.

If you start count from 0 at every iteration, then will you get the total count of all users online?

Also, do you really want to check if the user is equal to online? The user would be a name like â€śAlanâ€ť. How would that every be equal to online? Make sure you understand what online is with respect to the overall object.

1 Like

I want to count every true to get the users. I have made equal the access to the object to true. I should get the adding of every cycle.

``````
for(const user in users) {
let count=0;
if (user["online"]==true){
count+=1;
}
return count;

}

``````

You keep resetting the value of count to `0` before the â€śonlineâ€ť property is checked, plus you are only ever going to return a count of `0` or `1` because you only end up checking the first user name in the object before executing the return statement. Once a return statement is executed, the function is immediately exited.

1 Like

I tried it again. I do not understand the reason why it is misconfigured the cycles in order to get the final value.

``````function countOnline(usersObj) {
// Only change code below this line
let count=0;

for (const user in usersObj){

if (user["online"]==true){

let count=count + 1;
}
return count;
}
// Only change code above this line
}
``````

You still have the issue with the location of your return statement. You only check the fisrt user before returning a value and exiting the function.

1 Like

This is the last task that it is being asked by the challenge. The function `countOnline` should use a `for in` statement to iterate through the object keys of the object passed to it. But, I used them.

``````function countOnline(usersObj) {
// Only change code below this line

let count=0;

for (const user in usersObj){

if (usersObj[user]["online"]==true){

count=count+1;

}

}
return count;
// Only change code above this line
}
``````

It is done. I am thankful for your time.

``````function countOnline(usersObj) {
// Only change code below this line

let count=0;

for (let user in usersObj){

if (usersObj[user]["online"]==true){

count=count+1;

}

}
return count;
// Only change code above this line
}
``````
1 Like