Iterate Through the Keys of an Object with a for...in Statemen

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){
    usersOnline++;
  }   
 }
 return usersOnline;
}

console.log(countOnline(users));

What can be the problem?

always give link.
like https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property/
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:

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

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.

1 Like

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.

1 Like

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.

Hello. I changed location of my variable counter to local and join hasOwnProperty method to check but obliviously there is still a problem there. Could you look at this please. Thnx a lot…

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

function countOnline(obj) {
if(users.hasOwnProperty(ā€˜Alan’, ā€˜Jeff’, ā€˜Sarah’, ā€˜Ryan’) === true) {
for (let user in obj) {
let counter = 0;
if(obj[user].online == true){
counter++;
}
return counter;
};
}
}

console.log(countOnline(users));

First, the if statement that hard codes the name of each user is not appropriate for this function.
You are given an obj which has all the users already so checking for them to exist is not needed and makes for bad code in general. (Your function is supposed to count the users who are online, that is all)

Examine where you placed your counter variable. Is it in a good position? Will it be incremented every time your for loop sees an online user? Will it stay incremented every time the for loop, loops?

This is the solution I have given. But the count is returning zero. Can anybody help?. Thanks.

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

function countOnline(obj) {
// change code below this line
var count = 0;
for (let usernames in obj) {
if (usernames.online == true) {
count++;
}
}
return count;
// change code above this line
}

console.log(countOnline(users));

Hey @RohithSriram , Use the ā€œAsk for helpā€ button on challenge page whenever you need help with any of the challenges. It formats your code properly and also gives link to your challenge instead of posting on someone else’s post.

Happy coding!!

Hi @aditya_p. I thought since someone else has already asked this question I thought I can use the same thread instead of a starting a new one. Can you help me out on this one? . I will follow your suggestion and a start a new thread. Thanks

I’m just curious if FCC knows that the ā€œGet a Hint,ā€ link on this challenge is broken, at least it is for me. Showing up as 404, not found.

3 Likes

Here is the solution

function countOnline(obj) {
var count = 0;
for (let user in obj) {
if(obj.hasOwnProperty(user)){
if(obj[user][ā€˜online’] === true){
count++;
};
}
};
return count;
}

console.log(countOnline(users));

1 Like

// my Solution
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};

function countOnline(obj) {
// change code below this line
let usersOnline = 0;
for(var ob in obj){
if(obj[ob].online){
usersOnline += 1;
}
}
return usersOnline;
// change code above this line
}

This is my solution. I hope you find it useful

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

function countOnline(obj) {
// change code below this line
let count = 0;

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

console.log(countOnline(users));

Look like the ā€˜Get a Hint’ page for this problem is broken. Below is the code that works!

function countOnline(obj) {
  // change code below this line
[spoiler]
let total = 0;
for (let user in obj) {
  if (obj[user].online === true) {
    total++;
  }
}
return total;
[/spoiler]
  // change code above this line
}

console.log(countOnline(users));