Tell us what’s happening:
Solution looks correct, is nearly identical to solution offered in ‘Get a Hint’ thread, but does not pass tests. Using console.log() to print out test results reveals:
0
0
1
These results don’t make sense to me, given the coded
Your code so far
function countOnline(usersObj) {
// Only change code below this line
let i = 0; {
for (let users in usersObj) {
if (usersObj[users][online] === true)
console.log(i++);
}
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
function countOnline(usersObj) {
// Only change code below this line
let i = 0;
for (let users in usersObj) {
if (usersObj[users].online === true)
return i++;
}
Still yields same results (0, 0, 1), correct sequence of answers being (1, 2, 0).
function countOnline(usersObj) {
// Only change code below this line
let i = 0;
for (let users in usersObj) {
if (usersObj[users].online === true)
i++;
}
// Only change code above this line
}
The instructions require you to return your value stored in i, but you cannot return inside of the for loop.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
You’re mostly there, you just need to put your return inside your function but after you have finished counting.
Sorry if I’m giving you just little nudges. You’re so close that I’m trying to nudge you without giving you the answer so you can have that ‘ah-ha’ moment!