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
}
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.
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;
}
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