For In statements!

Ok, so I have been struggling with this problem for a few days! I already looked up info on"for in" statements on MDN and I think I am close and I feel like there is some silly mistake I am not even seeing.


function countOnline(obj) {
  // change code below this line

  for (let user in obj) {
    if (obj[user].online === true) {
      result++;
    }
  }
  return result;
  // change code above this line
}
console.log(countOnline(users));

Your browser information: Chrome - w/ latest updates

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

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

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement

it doesn’t look like you initialized result

do something like let result = 0 before the for

If you’re a ninja i think there’s a way to init inside the parentheses but I always screw that up.

Got it! It was that plus I used “user” in the function/for in to mirror the example but it really had to be “countOnline”

Thanks=]

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.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums