For...in statement clarification

Hi, I am struggling with this exercise:

Question 1: In the end I just copied the solution in my own code, and I understood it better once I saw the solution - but it still doesn’t let me pass - is there something wrong with what I have written?

Question 2: I am confused how I am supposed to see the result number come up in console log? For example here it should come up with the number 1, because there’s one user online - but where can i see that?

let user = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

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

  1. You are referencing obj, but you don’t have any variable by that name.
  2. If you add a console.log then you will see it print in the console. To see the console, open your browser’s dev tools (F12) and go to the “console” tab.
1 Like

Thank you - I changed my variable to userObj which works!

But the console.log isn’t showing up in console tab? See below screenshot.

You must click Run to see the results. Also, always post actual code (not screenshots) in forum posts, unless instructed otherwise. It makes it easier for us to test your code.

1 Like

Sorry here is my actual code:

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

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

When I click Run it comes up with this in the console :

ReferenceError: users is not defined
DevTools failed to parse SourceMap: https://www.freecodecamp.org/assets/service-worker-5143f3dcc5d4f1827a851e30664e7ccd94e73a88847af8766f86ff8744323b49.js.map
test-evaluator.fe115…d617ada2e635.js:569 ReferenceError: users is not defined
test-evaluator.fe115b04d617ada2e635.js:569 ReferenceError: users is not defined
test-evaluator.fe115b04d617ada2e635.js:569 ReferenceError: countOnline is not defined
test-evaluator.fe115…d617ada2e635.js:569 ReferenceError: users is not defined

Any ideas as to what the issue is?

As it says in the error message: users is not defined.

1 Like

If you respect the // Only change code above/below this line comments, you will be able to fix your code. You have the right pieces there, but you are confusing the editor by putting random stuff in places you were told not to put code.

2 Likes

Thanks so much I got it!

Sorry I don’t understand - where have I put code where I’m not supposed to?

I did add the object section before it and console.log() after it but how else am I supposed to view my result? Is there a better way? Sorry if I am not getting it… I am very new to this

Your changes must be in between

  // Only change code below this line

and

  // Only change code above this line

or the tests will not pass.

function countOnline(usersObj) {
  // Only change code below this line
// THIS IS THE ONLY PLACE YOU SHOULD BE TYPING!!!!
  // Only change code above this line
}

You can put other stuff in when you test your code yourself, say on repl.it, but the tests will not pass if you write code outside of those two comments.

1 Like

Ok - many thanks! :slight_smile:

1 Like