Iterate Through the Keys of an Object with a for...in Statement(why is wrong)

Tell us what’s happening:

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

function countOnline(obj) {
  // change code below this line
  let a = 0;
  for(let user in obj){
    a++;
  }
  return a;
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 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

You didn’t read the instructions properly for sure ! Please always read the instructions and pay good attention to details.
So the thing is there are two matters going on ,first it says *loop through the users in the users object
which you implemented here

for(let user in obj){
    a++;
  }

But there was another condition that you missed in the challenge is *return the number of users whose online property is set to true . so in your code you did nothing to implement this .
So here lets do what it says , return the users with online property set to online.

function countOnline(obj) {
  let count=0;
  for(let user in obj){
    if(obj[user]["online"]===true){
      count++;
    }
  }
return count;
}

And thats it

3 Likes