Like @lubodrinka mentions, your function is not returning the value the instructions told you to return. You are returning the number of users instead of a value of true or false indicated whether or not the users object contains all four names (Alan, Jeff, Sarah, and Ryan) as keys.
Weāve defined a function, countOnline; use a forā¦in statement within this function to loop through the users in the users object and return the number of users whose online property is set to true.
It appears @camper figured out which challenge the OP was inquiring about.
@newToJS In the future, please click on the Ask for Help button on the challenge. Doing so will automatically populate the challenge name, url and all of the code you have into a post. This will aid in getting you a faster and correct response instead of us making a guess (which I guessed wrong) of the challenge.
instead of making usersOnline be a global variable, try putting it inside the function
that way when someone calls the function multiple times the value of usersOnline will be reset back to 0
Also I would suggest changing the (let user in users) to (let user in obj) since that is what is being passed to you
and if the function gets called with a different object, your code will not work.
The problem is the global variable declared outside the function. The FCC tests run your function and can access any global variables you have created outside the function. So what ends up happening is usersOnline will start a new test with the value it had at the end of the last test.
Hello. I changed location of my variable counter to local and join hasOwnProperty method to check but obliviously there is still a problem there. Could you look at this please. Thnx a lotā¦
First, the if statement that hard codes the name of each user is not appropriate for this function.
You are given an obj which has all the users already so checking for them to exist is not needed and makes for bad code in general. (Your function is supposed to count the users who are online, that is all)
Examine where you placed your counter variable. Is it in a good position? Will it be incremented every time your for loop sees an online user? Will it stay incremented every time the for loop, loops?
function countOnline(obj) {
// change code below this line
var count = 0;
for (let usernames in obj) {
if (usernames.online == true) {
count++;
}
}
return count;
// change code above this line
}
Hey @RohithSriram , Use the āAsk for helpā button on challenge page whenever you need help with any of the challenges. It formats your code properly and also gives link to your challenge instead of posting on someone elseās post.
Hi @aditya_p. I thought since someone else has already asked this question I thought I can use the same thread instead of a starting a new one. Can you help me out on this one? . I will follow your suggestion and a start a new thread. Thanks
function countOnline(obj) {
var count = 0;
for (let user in obj) {
if(obj.hasOwnProperty(user)){
if(obj[user][āonlineā] === true){
count++;
};
}
};
return count;
}
function countOnline(obj) {
// change code below this line
let usersOnline = 0;
for(var ob in obj){
if(obj[ob].online){
usersOnline += 1;
}
}
return usersOnline;
// change code above this line
}
Look like the āGet a Hintā page for this problem is broken. Below is the code that works!
function countOnline(obj) {
// change code below this line
[spoiler]
let total = 0;
for (let user in obj) {
if (obj[user].online === true) {
total++;
}
}
return total;
[/spoiler]
// change code above this line
}
console.log(countOnline(users));