I don't understand the for...in Statement Challenge

Tell us what’s happening:
I don’t understand why my code is not working and to be honest, I wouldn’t really understand why it works if it would work :smiley:

Your code so far:

function countOnline(usersObj) {

  // Only change code below this line

let onlineCount = 0;

for (let user in usersObj) {

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

  onlineCount++;

  return onlineCount;

  }

  else

  return 0;

}

  // Only change code above this line

}

Challenge: Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

Let’s look at this, your code will always return or 1 or 0, once a return statement is met the function stops and output the value

When are you able to say how many users are online? Here you are just checking the first one

1 Like

You’ve worked out the logic and solved the problem.
It’s just your return statements that are the issue.

  • You only need one as you have zeroed the variable and so already accounted for the zero online possibility.
  • It needs to be out of the loop (how else can it count more than one person at a time).

You also don’t need the else statement -which is short a pair of {} anyway.

1 Like

sometimes to understand a function behavior, that works with data passed to it, its good practice to make a mock data and pass it to the function, see how it behaves. You could even use the already present data objects, with which the challenge tests your code, which also show the expected result. For example { Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } } should return result of 2. { Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } } should return 0. You can call your function with one of those objects and console.log what it returns. (console.log(countOnline(testObj))). You should even place additional console log statements in your code, on problematic points, to see in details how it behaves. As the challenge description tells, your function should count how many users(keys/props) in an object, has their online property set to true. You obviously, want to first create a variable to store that count as you did and then add up, each time online is true. Only once you loop thru the entire object properties(user names), you should return the count value. In your case, you return a value on the first for...in iteration. Remember how “return” works. It exits the function and gives us a value, thus breaking the for…in loop with it. Any code past the return statement is not ran.

1 Like

//let isonlineObj = {
//a : {isonline : true},
//b : {isonline : false},
//c : {isonline : true},
//d : {isonline : true}
//}

function countOnline(usersObj) {

// Only change code below this line

let onlineCount = 0;

for (let user in usersObj) {

if (usersObj[user].isonline === true) {

onlineCount++;
//return onlineCount;
}
//else
//return 0;

}
return onlineCount;
// Only change code above this line

}
//countOnline( isonlineObj)

Please don’t hijack the threads of others. If you have a question on this challenge, please use the Ask for Help button and create a new topic for your questions. Thank you.

Ha, thank you all! All of the 3 answers kinda helped me, I read over all the answers and then I figured it out quite fast.