Problems with Basic Data Structures Iterate Through the Keys of An Object with a For in Statement

Tell us what’s happening:
Good afternoons to everyone,
I am having problems of understanding this challenge could anyone
can help me to understand it.

Your code so far


function countOnline(usersObj) {
 // change code below this line
let result = 0; 
for (let user in obj){
 if (obj[user].online === true){
   result++;
 }
}
return result;
}
console.log(countOnline(usersObj)); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 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

What part is causing you problems?

Dear @ArielLeslie,
Good afternoons,
Thank you for your kind reply, i don´t know how to define the function countline so it can return 1
when the object Alan:{ online:false},Jeff:{online:true},Sarah:{online: false}}
thank you in advance dear @ArielLeslie.
Best regards,
Ivonne

1 Like
for (let user in obj){

obj is not defined anywhere in your function.

Dear @ArielLeslie,
Thank you i will define it, i´ve forgotten it :pensive:

Tell us what’s happening:
Good afternoons to everyone,
I´m trying to solve this challenge although
it seems to me that is fine, sadly
the challenge gives me some mistakes , if someone
can help me please i will thank you so much:pray:

Your code so far


function countOnline(usersObj) {
// change code below this line
let result = 0;
for (let user in usersObj) {
  if(usersObj[user].online == false) {
    result++;
  }
}
return result;
// change code above this line
}
console.log(countOnline(users));
// change code above this line


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 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

Your latest code has two issues. The main issue is you are counting the number of users who have an online property set to false and that is not what the challenge instructions tell you to do.

The other issue (which is hidden from the FCC console but not the browser’s console) is your console.log statement references a users variable, but there is no such variable defined in the global scope (where the console.log statement resides). This is causing the code to fail.

1 Like

Dear @camperextraordinaire,
I thank you so much for the help , i erase the variable users and let it empty and also i
changed the value false to true, thank you again, i was getting crazy with it :pray::pray::pray:

Have a Blessed day and thanks again @camperextraordinaire

1 Like