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
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
}
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”
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.
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
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.
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.