Iterate Through the Keys of an Object with a for...in Statement Can someone please help me understand what this challenge wants exactly?

Tell us what’s happening:
the challenge is asking for this:
The function countOnline returns the number of users with the online property set to true
with my code, it returned 2 as it was the number of users with the “online” property set to “true”. my code works but it’s not accepted.

Please forgive me if I don’t look at the other posts, I want to solve it by myself if I can (I hope that makes sense despite me being here)
What exactly is this challenge asking for? There’s 2 people online, through for in loop (which is what the challenge wants) I got 2, but still it’s not working.

Your code so far


let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

let aa = 0;
function countOnline(obj) {
  // change code below this line
  for (let x in obj) {
     for (let y in obj[x]) {
       if (obj[x][y] === true) {
         aa++
         
       }

       }
       
     }
  return aa;
  // change code above this line
}

console.log(countOnline(users));

//var person = {fname:"John", lname:"Doe", age:25}; 

//var text = "";
//var x;
//for (x in person) {
//    text += person[x] + " ";
//}

//console.log(obj[x][y]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/-iterate-through-the-keys-of-an-object-with-a-for---in-statement/

If you move the line let aa = 0; to inside the function, it will accept your answer.

But you can also improve your code, for example, instead of having the second for loop and look every property of the object, you can look only at what interests you, which is the online property, do if (obj[x].online) aa++;.

weird, when I move let aa = 0; inside the function, it returns “aa is not defined” what is happening?? O.O

sorry, the position of declaration was wrong, it’s alright now. But I don’t get it, the result is the same, I got 2, I know it could be a problem if I declare it outside, but in this case, it’s not. So now I’m confused if my code was right or if the challenge just hit a snag.

Regarding you suggestion
(obj[x].online) aa++; .
Thanks a lot! :slight_smile: I still don’t get a lot of these shorthand codes but using .online specifically instead of doing another for in loop is a really great advice! Thanks, I still miss a lot of the basic stuff :slight_smile:

Ya, it should work if it calls your function just one time. But if it is called more than one time, then aa is never reseted to 0, which won’t work.

I still don’t get a lot of this so I’m not sure how much I can remember but thanks for taking the time to teach me :slight_smile: I appreciate it!