Iterate Through the Keys of an Object with a for...in Statem

Tell us what’s happening:
it is returning count = 0;

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
  var count = 0;
  for(let user in obj)
  {
    if(user.hasOwnProperty(online) === true)
    {
      count++;
    }
  }
  return count;
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0.

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

Hope the following code snippet will help you to understand the object “users”.

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

for(let user in users)
{
	console.log("user =>", user, "and type of user is", typeof user);
}

console.log("============================");

for(let user in users)
{
	console.log("users[user] =>", users[user], "and type of users[user] is", typeof users[user]);
}

/*
==================
Here is the output
==================
user => Alan and type of user is string
user => Jeff and type of user is string
user => Sarah and type of user is string
user => Ryan and type of user is string
============================
users[user] => { age: 27, online: false } and type of users[user] is object
users[user] => { age: 32, online: true } and type of users[user] is object
users[user] => { age: 48, online: false } and type of users[user] is object
users[user] => { age: 19, online: true } and type of users[user] is object
*/

Happy coding.

A couple things going on here.

function countOnline(obj) {
  // change code below this line
  var count = 0;
  for (let user in obj) {
    if (user.hasOwnProperty(online) === true) {
      count++;
    }
  }
  return count;
  // change code above this line
}

First, user in the context of your for…in statement, is just the name of each user (the key) so your string “Alan” will never have its own property of “online”. to get at the user objects inside the users object, you would need to pass that key to it: obj[user].

Second, even if user did represent the whole user object, passing online like that with no quotes, the interpreter will not see the online property of users/obj but rather a variable named “online”. Which, since you never defined online will have the default value of undefined (go figure.) Thus your test, user.hasOwnProperty(online) === true will always be false, because you are essentially asking if each user has the property “undefined”, which none of them do thus never incrementing count. in order to pass online as a property here you need to put it in quotes, thus passing the string for online: "online"

Third, but even if you fix that, you still won’t have it. Every user in users has the property “online.” So user.hasOwnProperty("online") === true will always be true, always incrementing count.

What you are wanting to check for is whether the online property of each user is set to true. I feel if I give you much more, I’ll give it away, but hopefully this will set you in the right direction. Make sure to read the instructions carefully and review their sample code.