Iterate Through the Keys of an Object with a Statemen

Iterate Through the Keys of an Object with a Statemen
0.0 0


Hi Campers!
This is my 1st post )

I stuck with the challenge. Actually, I solved it but I the FreeCodeCamp test validation system does not let me pass.

This is my code, which returns 2 which is correct.

let users = {
  Alan: {
    age: 27,
    online: false
  Jeff: {
    age: 32,
    online: true
  Sarah: {
    age: 48,
    online: false
  Ryan: {
    age: 19,
    online: true

let usersOnline = 0;

function countOnline(obj) {
  for(let user in users){
  if(obj[user].online == true){
 return usersOnline;


What can be the problem?


always give link.
your code is not a point of task
Finish writing this function so that it returns true only if the users object containsall four names, Alan, Jeff, Sarah, and Ryan, as keys, and false otherwise.


Like @lubodrinka mentions, your function is not returning the value the instructions told you to return. You are returning the number of users instead of a value of true or false indicated whether or not the users object contains all four names (Alan, Jeff, Sarah, and Ryan) as keys.


@lubodrinka, @randelldawson, I think @newToJS is referring to this challenge:

Where it states:

We’ve defined a function, countOnline; use a for…in statement within this function to loop through the users in the users object and return the number of users whose online property is set to true.


It appears @camper figured out which challenge the OP was inquiring about. :smile:

@newToJS In the future, please click on the Ask for Help button on the challenge. Doing so will automatically populate the challenge name, url and all of the code you have into a post. This will aid in getting you a faster and correct response instead of us making a guess (which I guessed wrong) of the challenge.


instead of making usersOnline be a global variable, try putting it inside the function
that way when someone calls the function multiple times the value of usersOnline will be reset back to 0

Also I would suggest changing the (let user in users) to (let user in obj) since that is what is being passed to you
and if the function gets called with a different object, your code will not work.


The problem is the global variable declared outside the function. The FCC tests run your function and can access any global variables you have created outside the function. So what ends up happening is usersOnline will start a new test with the value it had at the end of the last test.