Undifined when looping data

Tell us what’s happening:
I can’t figure out why data isn’t printed when I test on REPL.

Your code so far


function countOnline(usersObj) {
// Only change code below this line
let n = 0;
for (let user in usersObj){
  if (user.online === true){
    return n++;
  }
}
// Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) 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:

what’s your code on repl.it?

you need a function call and a console.log command

I do that, my code in repl.it same as code in above. I just insert the object in repl.it

do you get an error or something?

can you share your repl?

Can I know where I wrong? I’m so confused, I think that was the correct answer. Already compared with documentation didn’t wrong at parameter or something.

well, you get undefined

to debug more, add other console.log statements

for example like this:

function countOnline(usersObj) {
  // Only change code below this line
  
  let n = 0;
  console.log({n})
  for (let user in usersObj){
    console.log({user, 'user.online': user.online, 'user.online === true': user.online === true})
    if (user.online === true){
      console.log(user + ' is online')
      return n++;
    }
  }
  // Only change code above this line
}

Allright, ill tried first. Thank you :smiley:

as you want to learn to debug, I’m just giving hints on how to do that, if there is something you do not understand, feel free to ask

Yeah i have question, can you please give me a hint why my repl.it at line 6: console.log(user.online) give me undefined?

What should i read more? this problem should be here.

a string doesn’t have the online property

new hint: if the string doesn’t have the online property… what does?

advice: do not use the return statement on anything you are testing, that stops the function, use console.log commands, like console.log(typeof(user))

1 Like

Damn, i wont click those blurred :joy:
Thank you @ieahleen, i already figure out how to do that. MDN is the best :joy:

for what I see your code, it’s an “almost”

console.log output is undefined, so data is undefined and data.online gives an error

Yeap, but what should i do is write a obj[variable].target

yes, that’s better

just do not assign console.log to anything, doing data = console.log() is pretty much the same as doing data = undefined

1 Like

your return statement should outside for loop