Challenge asks: return the number of users whose online property is set to true.
I know how to return but this return wants me to count boolean values of an object within an object multiple times and return a value? Do I convert true to a number and return that number? How do I convert the amount of true’s to a number?
function countOnline(usersObj) {
// Only change code below this line
for (let user in usersObj) {
if (usersObj[user].online===true) {
return
}
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
No
the return should be the last line
in any function
even though it is often
found in other place
sometimes even in multiple
places . Good Practice Demands
only one return only on the last line
at the top of your code set apple to zero
each time in the loop that the condition
is true add 1 to apple
this will result in the same number
in apple as there are users online
in the last line return apple;
So I tried putting let apple=0; in the loop and the if statement and then I just put in in the top of the function to see how the scopes would work. None of it is working
function countOnline(usersObj) {
// Only change code below this line
let apple=0;
for (let user in usersObj) {
if (usersObj[user].online===true) {
apple + 1;
return apple;
}
}