Iterate Through the Keys of an Object with a for...in Statement code check

Tell us what’s happening:
I’m really not trying to look at the solution anymore until i figure out the problem myself. I created the sum variable so that the boolean ‘true’ can be converted to a number. Can anyone help give me some guidance as to what I might be doing wrong. Thank you!

Your code so far

 function countOnline(usersObj) {
  // Only change code below this line
var sum = 0
for (let online in usersObj)
if (online === true)
{return sum = sum + 1}
else
return 0

  // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

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

Link to the challenge:

The very first time your code encounters a return statement, the function will stop executing.

ah, i see. here is my adjusted code

function countOnline(usersObj) {

// Only change code below this line

var sum = 0

for (let online in usersObj)

if (online === true)

{sum = sum + 1}

else

return sum

// Only change code above this line

}

You are getting closer, but your else clause still includes a return. Do you need that else clause?

I would add some {}s for all of your loops, if-elses, etc to help you more clearly see what’s going on.

don’t you need an else clause if you use an if statement.

thanks so much for your responsive help by the way

No, there is no requirement to use an else or else if statement with an if statement. You only use the logic syntax needed. Nothing more. Nothing less.

1 Like
function countOnline(usersObj) {

// Only change code below this line

var sum = 0

for (let online in usersObj)

{if (online === true)

{sum = sum + 1}

}return sum

// Only change code above this line

}

im so close but its still not working. sorry for asking so many questions im just not sure how else to trouble shoot this and want to make progress

function countOnline(usersObj) {
  // Only change code below this line
  var sum = 0

  for (let online in usersObj) {
    console.log(online); // what is online?
    if (online === true) {
      sum = sum + 1
    }
  }
  return sum
  // Only change code above this line
}

// test the function
countOnline({ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } });

Does adding this console.log help you see what’s going on here? You need to do something more complicated with this variable.