Iterate Through the Keys of an Object with a for...in Statement - issue with output

Tell us what’s happening:
Not getting this at all, why is this wrong?

// running tests
The function countOnline returns the number of users with the online property set to true
// tests completed

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(obj in users) {
       if (users.hasOwnProperty('online') === true) {
         return true;
       } return false;
    } 
  
    // change code above this line
  }
  console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 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/

users is an object, which has as properties “Jeff”, “Sarah”, etc - so your condition can never be true

but, other than that, consider that once the code meet a return statement, the function returns that value and stop executing, so even if you had code to check the inner objects, you would just be checking the first one - plus, you don’t need to check if they have the property, but if their online property is set to true

and then return a number that count how many are online, not a boolean

and this?

  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(obj in users) {
      //  if (users.hasOwnProperty('online') === true) {
      //    return true;
      //  } return false;

      const checkStatus = users.filter(prop => prop!== false);
      if(checkStatus){
        return true;
      }
    } return false;
  
    // change code above this line
  }
  console.log(countOnline(users));```

Try breaking it down to the smallest steps, forget JavaScript

Just write the steps (pseudo-code)

This is a loop - do you know what is obj and what is users?

this is what my function looks like, my understanding is that we are using obj to represent each individual property with the object users but it seems to always return false and not return all the objects that is true i tried t also use filer method, i guess i still have an issue with my thought logic process.

function countOnline(obj) {
    // change code below this line
    for(obj in users) {
     // if the online property === true display the value
     if(obj === true){
       return obj;
       //console.log(obj);
     } return false;
  
    // change code above this line
  }

}
  console.log(countOnline(users)); 

So, users have properties Jeff, Sarah etc

So try maybe console.log(obj) in the loop and see what that is before trying to do anything

Also, you may want to change name there, because obj is also the function parameter

when i add console.log(obj) within the for loop it outputs Alan

Exactly. You want to evaluate the property of online within the obj, instead of the obj itself.

this is now how i have rebuilt my function however i get the messages:

** Cannot read property ‘online’ of undefined**
Cannot read property ‘online’ of undefined

    // change code below this line
    for(let user in obj) {
      //console.log(users[obj]);
      // if the online property === true output the value
     if([obj].user.online === true){
       return [obj].user.online;
       //console.log(obj);
     } return false;
  
    // change code above this line
  }

}
  console.log(countOnline(users)); ```

you need to remember a couple of things: that a return statement returns a value from the function and exit the function and everything after it is not executed, and also how to access properties in objects
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/return-a-value-from-a-function-with-return
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables