Check if an Object has a Property - failed one test

Tell us what’s happening:

Alright, so I cannot pass the second test probably due to not returning these 4 properties as true. And if I change my codes from (users.hasOwnProperty(obj)) into (users.hasOwnProperty('Alan', 'Jeff', 'Sarah', 'Ryan')) it will work, but the last three tests will not pass. What should I do?

Please note that I do not wish to see the solution. Just point me out to correct direction.

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

console.log(isEveryoneHere(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36.

Challenge: Check if an Object has a Property

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

hasOwnProperty accepts only one argument, any other is ignored - with this try to figure out what you need to do. You are checking just if Alan is there

did you found somewhere written that you can use more than one argument with that method?

1 Like

One thing I don’t get it is why will return false not work? If I understand correctly, my code currently checks if Alan is there only. I thought using the && operator will supposedly includes the rest of the properties, but looks like it does not solve anything.

Could it be that I didn’t add the properties to if..else statements properly?

if ('Alan' && 'Jeff' && 'Ryan' && 'Sarah' in users)

here you are checking if obj is a property of users, it is not possible as obj is an object, users is actually the same object (passed in the function)

in this case you are just checking if Sarah is a property. The && is executed last, so first step the line is evaluated as 'Alan' && 'Jeff' && 'Ryan' && true/false, strings are all truthy so everything becomes true or false just depending on if Sarah is a property of the object or not.
in will also check one property at a time

1 Like

I see. I see.

So the point here is my code currently checks one property at a time. Which means I have find a way to include other properties as well. Using the in (since I prefer less syntax anyway) and adding the && for each property will do the trick.

Frankly speaking, whenever I see parameters and arguments used in the challenge, I panic because I have no idea how it works.

Thank you, @ilenia! That helped me a lot!

1 Like

glad I could be of help! Happy coding!

1 Like