Check if an Object has a Property

Check if an Object has a Property
0.0 0

#1

Tell us what’s happening:

Your code so far


let users = {
  Alan: {
    age: 27,
    online: true
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: true
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function isEveryoneHere(obj) {
  // change code below this line
    return obj in users;
  // change code above this line
}

console.log(isEveryoneHere(users));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property


#2

You’re not really doing what it’s asking you.You need to create an if loop, that uses the .hasOwnProperty to check if the names are part of the obj that is passed in. (the obj is the users object WITH the names in your code editor.) You’re basic structure is supposed to look like

if(true){
    return true;
}else{
    return false;
}

where the first ‘true’ condition is the bool variable from obj.hasOwnProperty will give you when you check the names.


#3

There’s a couple things wrong with this code.

  1. you cannot return obj in users, because the in operator is used for iteration (looping)
    • the syntax you want is for 'var' in 'obj'
    • the for starts the loop
    • ‘var’ is the name you assign to the current property in the object
    • obj is the object you are iterating over (looping over)
  2. Since the var variable is the name you assign for the property key during iteration, you can’t place the parameter (obj in this case) there. It should be switched with users