HELP! Iterate Through the Keys of an Object with a for...in Statement

HELP! Iterate Through the Keys of an Object with a for...in Statement
0

#1

Tell us what’s happening:

I have seen some other posts on this particular problem and they assign values to terms before entering the for…in formula. I thought that there was a more basic way to go about this and below I’ve tried but have not been successful.

Help, por favor.

Your code so far


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
for (let user in obj) {
   if(obj.online === true){};
}
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

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

Link to the 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


#2

By “basic” I mean a more concise code. Just the for…in statement in very little else.


#3

dot notation won’t work
e.g obj.Alan.online

You need to use obj[Alan].online


#4

You are trying to refer to a property named “online” in the obj object, but it only has property names like “Alan”, “Jeff”, “Sarah”, and “Ryan”.


#5

What I think that I’m doing here is iterating users through obj with the condition that obj must be true and then console.log those users that meet this requirement.

I’m not passing the test. Where am I wrong?



function countOnline(obj) {
  // change code below this line
for (users in obj) {
   if(obj === true){
     console.log(users);
   };
}
  // change code above this line
}

console.log(countOnline(users));

#6

Again, you are referring to obj, which is the object which was passed into the function. In your for in loop you declares a users variable. What do you think users represents in each iteration of the for loop? If you are not sure, then I suggest you add a console.log statement to check it and view your browser’s console (Ctrl+Shft+J in Chrome).


#7

I’m confused by the different variables “users” and “user”. Both of them, in my console.log, are the same objects. obj comes out to undefined.

So, my assumptions are as follows:

ASSUMPTIONS:
user indicates an individual user within the users object
obj is a parameter of the function which requires that the user equal true in regard to online status.
The users object is too big to use in the function and would exceed the maximum stack size.


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 user = "";
for (obj in user) {
   if(user === true){
     return user;
   }
}
  // change code above this line
}

console.log(countOnline(users));


#8

Your syntax of the for in loop was actually correct in your last post.

Remember, when the syntax for a for in loop is:

for (let objectProp of objectName) {

The variable name (i.e. objectProp) on the left of OF represents each property name in the the object (i.e objectName). You can name this variable anything you want, but I typically name it something which describes what it represents. I would personally name it user or userName, because it will be one of the names (Alan”, “Jeff”, “Sarah”, or“Ryan).

I am going to use a different example below to drive home my point and then let you figure out what you need to write for this challenge.

let dogs = {
  Shadow: {
    age: 5,
    hasFleas: false
  },
  Skippy: {
    age: 2,
    hasFleas: true
  },
  Bover: {
    age: 10,
    hasFleas: true
  }
};

In the code above dogs is the object. If I want to display a message to the console for each dog object which has an age property value greater than 4, then I could write:

for (let dogName in dogs) {
  if (dogs[dogName].age > 4) {
    console.log(dogName + ' is older than 4 years.');
  }
}

The above code displays the following in the console.

Shadow is older than 4 years.
Bover is older than 4 years.


#9

I’m returning “Jeff is online”, but not Ryan. Yet, in my console.log it is returning the entire users object along with “Carl”, which I understand is an error from other posts I’ve read.

In what section of my code do I need to fix my syntax?


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 user = "";
for (user in obj) {
   if(users[user].online === true){
     console.log(user + " is online");
   }
}
  // change code above this line
}

console.log(countOnline(users));
 

#10

Look at your browser’s console (Ctrl+Shft+J in Chrome). Your function is returning undefined, because you do not have a return statement in your function. It is definitely not return the users object.

Remember the function is supposed to be returning a number (representing the number of users with an online property value of true.


#11

I got the answer using “counter”, but I wonder if there’s another way to do this. Is there a more streamlined way to complete this challenge?





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

console.log(countOnline(users));