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

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

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

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

You need to use obj[Alan].online

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

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

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

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