Is it right that I am using variable as x.online?

Is it right that I am using variable as x.online?
0

#1

Tell us what’s happening:

Is it right that I am using variable as x.online? Why I can`t sove this task?

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

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) 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

Check out this fiddle and see what the problem in your code is (It isn’t what you titled this post. x.online is the correct way to access the data.

Also, check this out:


#3

Updated fiddle, if you aren’t comfortable with the console yet (you should get really comfortable with it, but hey, I am a completionist):

http://jsfiddle.net/sqakjeom/1/


#4

Do you mean that I can use like this if(x.online)?


#5

Inside your for loop, x will be a string (i.e. “Alan”, “Jeff”, “Sarah”, or ‘Ryan’. These are property names of obj. Think about how you can leverage x and access a specific object inside obj with x to get the value of the inner object’s online property.


#6

Sorry, @RandellDawson is correct. I noticed a different error and focused on that instead of addressing your object of objects. You should take his hint about how to access the relevant member.