Check if an Object has a Property, why my code not working

Tell us what’s happening:
Can someone tell me why I kept getting an X on

The function isEveryoneHere returns true if Alan , Jeff , Sarah , and Ryan are properties on the users object

I tested my code individually and the result is expected so what gives?

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 ? true : false
   return users.hasOwnProperty(obj) ? true : false
  // change code above this line
}

console.log(isEveryoneHere(users)); // false

console.log(isEveryoneHere('Alan')); // true
console.log(isEveryoneHere('Jeff')); // true
console.log(isEveryoneHere('Sarah')); // true
console.log(isEveryoneHere('Ryan')); // true
console.log(isEveryoneHere('Test')) // false

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15.

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

What you are missing is that what gets passed to the function isEveryoneHere is an object and not a string. The obj parameter represents the object passed to the function. The hadOwnProperty method is used on objects. You pass a value (typically a string or number) which represents the property you are checking for existence.

1 Like