If you ask about why your code give you an error, here is the answer:
Firstly, you should understand what hasOwnProperty() and in do in object, both of them wanted to know if an object has a specific property or not.
Secondly, to check one object by these two syntax you should do like this:
users.hasOwnProperty('Alan'); //this by using hasOwnProperty()
'Alan' in users; //this by using in
// both return true
After understanding these two important things, we will going to your code,the problem in your code is because you check for one name in users object,so, you need to check all names by using && operator.
If you pinned in example, you can check the answer from here, good luck and have a happy coding
function isEveryoneHere(obj) {
// change code below this line
if ('Alan' in users == true && 'Jeff' in users == true && 'Sarah' in users == true && 'Ryan' in users == true)
{
return true;
}
else
{
return false;
}
// change code above this line
}
console.log(isEveryoneHere(users));
I wrote a solution for people who want to know how to build this with in and every():
function isEveryoneHere(obj) {
// the following names should be searched for
const names = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
// return true if every inner check returns true
return names.every((name) => {
// return true if the current element is in "obj" (= "users" array)
return name in obj;
});
// shorter syntax: return names.every((name) => name in obj);
}
obj is users it is a parameter and users is the argument of it ,so you sould do this : if(obj.hasOwnProperty(“Alan”)==true ) // also here you should not use the assignment operator (=) , rahter of that you have to use strict opertor (==) ,and finally you should add the same condition for the second property by the and operator (&&) to collect all the conditions as one condition so that they should all be true to pass the challenge .
This is my solution. I like better the “in” operator approach (bit more pythonic )
function isEveryoneHere(obj) {
// change code below this line
let names = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
//let check = names.map(x => users.hasOwnProperty(x));
let check = names.map(x => x in users);
return (check.includes(false) ? false : true);
// change code above this line
}
A simple solution using a basic for loop, using the .hasOwnProperty() method.
function isEveryoneHere(obj) {
// change code below this line
let names = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
for (let i = 0; i < names.length; i++) {
if (obj.hasOwnProperty(names[i])) {
return true;
} else {
return false;
}
}
// change code above this line
}
console.log(isEveryoneHere(users));
I am a little confused, as, if i’m not mistaken, the usage of in, every(), map() has not been introduced to us at this point. I was wondering if we are we encouraged to utilize subject matter which has not been covered in the curriculum yet.