Tell us what’s happening:
Anyone care to tell me whats wrong here ?
Your code so far
function countOnline(usersObj) {
// change code below this line
for(let user in usersObj){
let numOfCount = 0;
if(usersObj[user].online === true){
numOfCount++;
}
return numOfCount;
}
// change code above this line
}
console.log(countOnline())
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
your numOfCount variable will never be more than 1
the loop start, it is set the 0, then it may be or not be augmented by 1, new iteration, it is again set to 0… etc
plus, as you declared it with let inside the loop, it exists only inside the loop, the return statement is referencing a variable that was never declared in that scope
function countOnline(usersObj) {
// change code below this line
for(let user in usersObj){
if(usersObj[user].online === true){
let numOfCount;
numOfCount++;
}
return 0;
}
// change code above this line
}
also, you can’t use ++ on a variable with value of undefined
numOfCount needs to be defined in he same scope/block in which you want to return it
also, if you want to make changes to it based on a loop, you can’t define it inside the loop as in that way there is just a different variable for each iteration
If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.