Check if an Object has a Property, I want to learn!

Tell us what’s happening:
I passed the challenge. However, the challenge states: " Finish writing this function so that it returns true only if the users object contains all four names, Alan , Jeff , Sarah , and Ryan , as keys, and false otherwise." So if I change even one of the properties inside of the object being used here (I changed Ryan to Mark), shouldn’t the function I typed return false? I can only think that this is an error/typo in the challenge parameters. Which is fine, but either way I want to know how (if possible) to write a function that strictly needs all the properties I pass in to the hasOwnProperty() method.

Your code so far


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

function isEveryoneHere(obj) {
  // change code below this line

if (obj.hasOwnProperty(‘Alan’, ‘Jeff’, ‘Sarah’, ‘Ryan’)) {
return true
}
return false;

  // change code above this line
}

console.log(isEveryoneHere(users));    ``// Still returns true!``

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0.

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

1 Like

If you check the documentation for hasOwnProperty here:

You can see that it does not take a variable amount of arguments, only one. Since JavaScript doesn’t care if you pass too many, it only ends up checking the first one, Alan, which indeed exists and will return true as expected. If you want to use hasOwnProperty you will have to make a separate call for each check.

Edit: This seems like a false pass. This should be filed as a bug.

1 Like

Okay, that makes sense. Thank you!

1 Like