Basic Data Structures - Iterate Through the Keys of an Object with a for...in Statement

Tell us what’s happening:
okay so i have been stuck on this step for literal weeks now and i finally got it figured out to where its passing all but one of the tests.

The function countOnline should return 0 when the object

{ Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } }
is passed to it

what am i missing?

Your code so far

const users = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

function countOnline(allUsers) {
  // Only change code below this line
let count = 1;
for (let user in allUsers) {
if (allUsers[user].online === true) {
count++;
}
return count;
}
  // Only change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.58

Challenge: Basic Data Structures - Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

Looks like count should be initialized to 0, not 1.

let count = 0;

for (let user in allUsers) {

if (allUsers[user].online === true) {

count++;

}

return count;

}

// running tests The function

countOnline

should return

1

when the object

{ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }

is passed to it The function

countOnline

should return

2

when the object

{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }

is passed to it // tests completed

thats what its telling me when i change it to 0.

Your return statement is inside your for loop. So your function is always going to return on the first loop.

I feel kind of dumb to say this but I have absolutely no idea what you mean by that. I tried moving some of the text around and I still cannot pass this dang test… someone please help me out here with the correct code to pass this… I know you arent supposed to give away answers, but ive been stuck on this for literal weeks and I just cannot get it right.

Here was my go to whenever I was really stuck on one of these. His channel was extremely helpful for me. Iterate Through the Keys of an Object with a for...in Statement - Basic Data Structures - YouTube. He usually does a pretty good job of explaining the problem as he tries to solve it.

Let’s look at your for statement formatted nicely:

When your function goes through this for loop for the first time it will do the test in the if statement and the increase count by 1 if the test is true. Then it will execute the return statement. What does a return statement do? Based on that, how many times is this for loop ever going to run, regardless of how many users ar in the allUsers object?

THANK YOU SO MUCH!! THAT WAS UNBELIEVABLY HELPFUL!! I FINALLY PASSED!!! :heavy_heart_exclamation: :heavy_heart_exclamation: :heavy_heart_exclamation:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.