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

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

#1

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?


#2

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.


#3

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.


#4

@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.


#5

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.


#6

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.


#7

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.


#8

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));


#9

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?