Check if an Object has a Property - Should this be fixed?

Tell us what’s happening:
I have the answer for the problem. Even though the problem asked for ALL 4 names, I found that the function returns true even if the users object only contains 3 names in other words one of the name object is removed. This could be a potential bug in real application.
I would love to hear everyone’s thought on this.

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.hasOwnProperty('Alan', 'Jeff', 'Sarah', 'Ryan');

  // change code above this line
}

console.log(isEveryoneHere(users));
1 Like

hasOwnProperty takes one argument, it ignores the rest, it doesn’t matter what you put after ‘Alan’ in the example. If that is still the hint, then it’s wrong and I think there is an issue open to fix it.

1 Like

Thank you for the clarification.

  • The objective of this particular challenge is to teach you how to check if a property exists in a particular object or not. When I did this challenge not too long ago I discovered two ways to do it. The first way is the way you did it and the second way is:
'Alan' && 'Jeff' && 'Sarah' && 'Ryan' in obj; //returns true or false 
  • I wouldn’t say that you have picked up a bug. I see where you’re coming from but look at it from a conceptual way. It is hard to come up with the perfect example to teach small concepts like these. What is most important is to learn & know how to check if a property exists in an object for this challenge and you’ve already achieved that.
    Congrats :smiley:

No, it isn’t: none of the following work properly:

obj.hasOwnProperty('Alan', 'Jeff', 'Sarah', 'Ryan');
obj.hasOwnProperty('Alan', 'Jeff', 'Sarah');
obj.hasOwnProperty('Alan', 'Jeff');

Because hasOwnProperty checks for one property, not multiple properties. It’s an error in the examples given on FCC

1 Like
  • my apologies to you or anyone else who might have read my previous reply. You’re right DanCouper. the hasOwnProperty() method does not work within the context of this challenge especially in a case where you were to write something like:

obj.hasOwnProperty('Alan', 'XYZXMHKP'); //which shouldn't return true but it does

  • I think another function or example which would be more appropriate within the same context of this particular challenge could be something like this:
function areYouHere(obj, name) {
  return obj.hasOwnProperty(name);
}
console.log(areYouHere(users, "Alan"));//true
console.log(areYouHere(users, "Bob")); //false
  • Let me know what you think about my suggestion above. I’d appreciate your honest feedback about it - please don’t be too harsh if it’s not a postive one :sweat_smile:

I think it’s either you read what I wrote wrong or I wasn’t clear enough in my writing. I meant that I found an issue in FCC lesson that without being corrected could lead to buggy application written by students aka us thinking we understand it. It’s better and more efficient to learn things once and correctly than learn them in the wrong or unclear way.

Hi, all, joseph has definitely found an issue with the answer provided for this lesson. The Hint says you could use

return (users.hasOwnProperty('Alan','Jeff','Sarah','Ryan')) ? true : false;

But as Dan points out, hasOwnProperty can only take one parameter and it ignores the rest. So the hint is wrong (both versions) and it should be updated. I’m new to freeCodeCamp, how can we check what has been reported and submitted for fixing already?

For anyone else who stumbles upon this thread and is curious about different solutions, here is my suggestion and hopefully it helps someone:

function isEveryoneHere(obj) {
var props = ['Alan','Jeff','Sarah','Ryan'] // first declare an array of values you want to check
 
// change code below this line
  for(let i=0; i<props.length; i++){
    if(!obj.hasOwnProperty([props[i]])){ // loop through the array of values and check if value exists
    return false; // if not return false 
    } 
  } return true; // if everything checks out return true
  // change code above this line
}

I agree with @josephtrieutran that the solution offered in the hints page (https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property/)

does not accurately solve the problem because the test returns true if any one of the properties are present.

The following will be a more accurate way of making sure ALL the properties are contained. I intentionally wrote it this way to illustrate the two methods of running the checks (hasOwnProperty or in)

return (users.hasOwnProperty('Ryan')&&'Jeff'in users && 'Alan' in users && 'Sarah' in users)?
true:false; 

at this point you don’t even need the ternary operator… all that condition evaluate to true or false, so you can just return the condition

1 Like