Iterate Through the Keys of an Object with a for...in Statement= Unable to pass both the tasks in this challenge,

Tell us what’s happening:
I am passing in the first task but not in the second task as mentioned in the challenge, where did i go wrong ? I got only one green tick but the other one not yet ticked off in green, so, something is wrong with my result, wonder what ? thanks

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
  for (let online in users)
    if(users.hasOwnProperty('online')) {
      return  true;
    } else return false;
  // change code above this line
}

console.log(countOnline(users));


**Your browser information:**

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

**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

So you are iterating over the users object property keys, and for every iteration of the loop that key is stored in the online variable. (Maybe you want to use a different variable to make it clearer what it is holding?)

Then, the users object doesn’t have an online property, it’s properties are Sarah, Jeff etc. So the return statement that is executed is the one that returns false - the issue is that once something is returned the function stop executing, so you are not actually iterating over all object properties

You are returning a Boolean, but you need to return a number, the number of users that are online (online property set to true)

1 Like

You’ve definitely got a bunch of smaller things going on, and at least one conceptual hiccup. You should be able to replace your countOnline function with the one below, then work through the comments.

Definitely make sure you understand what’s going on (i.e. don’t just make it work!) or else the conceptual misunderstanding will bite you again and again later! :wink:

function countOnline(obj) {
  // You need some way to retain a number that represents the count of online users
  for (let name in users) // Your `for` needs curly braces around its actions
    console.log(name)  // Take a look at the info the for...in is providing
    console.log('users[name]:', users[name]) // and then review how to get
    console.log('users[name].age:', users[name].age) // data out of an object
    if( true ) { // based on the above, you'll need to change your if condition
      true; // and you'll need to iterate your numeric counter
    }
  // don't forget to close the curly braces in your `for` block
  return; // don't forget to return the numeric count
}
1 Like

I am able to crack it this time and I passed as well. I read couple of explanations on the theory and understand the concept now. Sometimes, it makes more sense to research a lot and understand oneself before asking for help from others …

function countOnline(obj) {
// change code below this line
var numOfUsers = 0;
for (let users in obj){
if (obj[users].online === true){
numOfUsers++;
}
}
return numOfUsers;
// change code above this line
}

console.log(countOnline(users));