Stuck with `for...in`. please help!

Stuck with `for...in`. please help!
0

Tell us what’s happening:

I followed the code in the instructions and I couldn’t get the challenge right. If I am doing something wrong in the challenge, please tell me.

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 user in obj){
return user.online;
}
  // 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_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 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

user is just a string, it is the property key of the object, but it is just a string - do you remember how do you access object properties using variables?

After that, the challenge is asking you to count the users that are online - you can’t put a return statement inside a loop as that would mean that you are stopping the loop at first iteration , and that wouldn’t return a number

I came up with this:

function countOnline(obj) {
  // change code below this line
  var output = "";
for(let user in obj){
output = user.online;
}
return output;
  // change code above this line
}

but I was still stuck :frowning:

As @ieahleen already told you, in this case user is variable with a string value like “Alan”, “Jeff”, “Sarah”, or “Ryan”. Review the following challenge for a hint of how to access the actual object for each name.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables

You are still not counting anything…
You need to return a number
I suggest heavy use of console.log to understand what’s going on

I can also suggest this tool: http://pythontutor.com/javascript.html

I got the code below:

function countOnline(obj) {
  // change code below this line
  var output;
for(let user in obj.online){
output = user;
}
return output;
  // change code above this line
}

but I don’t know what it returns and I still get the same result:
“The function countOnline returns the number of users with the online property set to true”

If you don’t know what it returns, then put a console.log statement to see what the value of output is before you return it.

console.log(output);

You were closer the first time with your for (let user in obj) {
You need to review the challenge I mentioned above so you can see how you could use the variable user (which contains a string) to access a particular user’s online property value.

I am hopelessly stuck. I don’t know if I had the wrong loop, or the wrong output.
:expressionless::dizzy_face:

Have you understood what the challenge is asking from you?

Do you understand what you are returning against what it is being asked?

Have you tried the pythontutor so you can see what your code does?

user is just a string, its value is a different property key of the object at each iteration- and I asked you if you remember how to access object property values using variables

Don’t worry, let’s go line by line here, but just a hint, you are using the right loop to solve the challenge.

As a human, you can count how many users are online by just looking at the users object. That’s basically what you need to do here, create a function that counts how many users are online, but unlike you the computer can’t see the whole data, it has to go line by line and check if an user is online. Whenever there’s an if, chances are you probably would need a if block statement.

So given that you have more than 1 user in your data, you will need a loop to access every user, and since you are working with an object, a for in loop is what you need here.

Instead of this:

for(let user in obj.online){
     output = user;
 }

Try this:

for(let user in obj){
 console.log(user)
}

alright, so I got the loop, but I don’t know what to put inside of it.
what I tried to do is test the properties of the users to see if they were online, but the console logged false on all four of them. If you could help me just find the properties, I might be able to pass the challenge.

for(let user in obj){
console.log(obj.hasOwnProperty(user.online));

}

Well, user is a string, so user.online doesn’t have any sense - user is a different property key at each iteration, it is just that the property key is in a variable. online is also a property key, but of the nested object, and this one is the exact name

do you remember the lesson on nested objects? and also on accessing property values using variables?
Check again these lessons:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-objects

I got this:

for(let user in obj){
output = obj.Alan.online + obj.Jeff.online + obj.Sarah.online + obj.Ryan.online;
}
return output;

but then the computer probably knew I was cheating. I returned 2, but I couldn’t pass the challenge.

Yes, the 2nd test uses a different array of users with different names and a different number of users with an online property of true.

You have to treat your function logic as a “black box”. It should not matter how many users sent to the function. All it should do is iterate through the objects and count the number of users with online property of true.

Let’s imagine that you have the following data:

const user = {name: "Peter", age: 25, isOnline: true }

If I ask you to write a function that takes a string as a parameter and log in the console part of the data of the user object (either the name, age, or online status), you will need to access the object data dynamically.

function logData(prop) {
 console.log()
}

logData('name') // expected output: Peter
logData('age') // expected output: 25
logData('isOnline') // expected output: true

There are 2 ways to access an object properties, using dot notation or bracket notation. Using dot notation here wouldn’t be useful, because you don’t know which property you are going to pass as an argument when you call your logData() function, it could be name, age or isOnline, you don’t know that. How would you do it?

Search on Google how to access properties in objects in JavaScript if you are bit confused, take a look at this article, it may help you clear your doubts.

something’s wrong with your function. the console.log() should be console.log(user.prop);
I get what you trying to say, @Gilbert1391

Here is a really useful article explaining the difference between bracket and dot notation. It should help you with this one.

The function is incomplete, it was meant that way so you could try to complet it.

I don’t know if you already solved the challenge, but if you still have doubts of how to approach this problem, I would highly recommend you to study how to access objects properties and the difference between bracket and dot notation. Once you understand that I think it’ll be easier for you to solve this challenge.

Loops and objects can be quite confusing at the beginning, so watch/read as many videos/articles as you need related to the topic.