Given below is my code but I am unable to pass the challenge. Need help please.
function countOnline(obj) {
// change code below this line
for(let user in obj) {
if(obj[user].online == true) {
console.log(user);
}
}
// change code above this line
}
What you were supposed to do in the challenge was to return the number of users whose online property is set to true, not to show them on the console. Simple steps you have to follow:
declare a variable inside the function with 0 value;
Thanks guys, I did it my way. I initialized an empty array and then pushed all the user into it and then return the length of the array. It did the trick…
like this
let arr = [];
arr.push(user);
return arr.length;
function countOnline(obj) {
// change code below this line
var count = 0;
for(let user in users){
if(user.online == true){
count += 1;
}
}
return count;
// change code above this line
}
user is a string, it doesn’t have the online property. what the for…in loop gives you is the keys of the object properties. So, how do you access an object property with variables?