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));
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.
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
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:
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
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.
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
}
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;