What am I doing wrong ? Check if an Object has a Property

Tell us what’s happening:

ReferenceError: Alan is not defined
I keep getting that error.
Whenever I try inputting all names or just use obj
Why is that? Can’t it access? did I make a grammar error?
The hints only giving me full solutions so I can’t use that

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) {
// Only change code below this line
if(users.hasOwnProperty(Alan, Jeff, Sarah, Ryan)) { //changing this to obj not working
"obj" in users;
return true;
}
else{
return false;
}
// Only 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/83.0.4103.97 Safari/537.36.

Challenge: Check if an Object has a Property

Link to the challenge:

Hello, Kora.

All of the arguments in this are interpreted as variable names:

hasOwnProperty(Alan, Jeff, Sarah, Ryan)

I assume you wanted to put the arguments as strings, but even that is not quite what the challenge is looking for. Maybe read the docs about hasOwnProperty

Hope this helps

2 Likes

It did a little but now im getting the error that
TypeError: users.isEveryoneHere is not a function

users.isEveryoneHere(obj);//changing this to an empty string or the string containing also results in the same error

'Jeff' in users;

'Sarah' in users;

'Ryan' in users;
''''

Well, let us start from the beginning…

For now, pretend the object users does not exist.

  • You are tasked with finishing the function isEveryoneHere.
  • You can see that it contains the parameter obj (which we can assume is an object literal).
  • You know you should probably use the hasOwnProperty method. The name is quite self-explanatory; it is a method called on an object, which checks is the object has the following property. Eg. myObject.hasOwnProperty('prop')

Now, users exists! So, how could you effectively look to see if each and every user object has a specific property?

1 Like

I’m quite new to Js and coding in general(just started few month ago),
though i might be able to help you understand your script and this challenge a bit better

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) {
// Only change code below this line

/*using users or obj currently(based on this script) makes no differents,yes
but i believe that the declared variable (obj) for the passed argument
is superior to calling the object itself(e.g users)
hence with the obj variable you can easily check any new object using
[console.log(isEveryoneHere(newUserlist)]as an example
which wouldn’t work with users still hardcoded in your function /
if(users/obj/.hasOwnProperty(Alan,Jeff,Sarah,Ryan)) {/here you want to change
your username variables into
strings
/
/

‘Alan’ in users; this i didn’t understand…you are calling string ‘Alan’ inside the
‘Jeff’ in users; function where you are asking if the passed object contains said
‘Sarah’ in users; string; i tried executing this command as i wasn’t sure of it’s
‘Ryan’ in users; use, to find that it does the same as your [if(…hasOwnProperty)]
where you called this; basically you ask if all “userName”-strings
exist and if [which only executes !IF! all do] you ask for everyone
individually again, this is double double code so you can
savley remove it.
I guess this is a result of the problem that you have,
the missinterpretation of Sky020s post that you need to change the
Alan,Jeff… usernames into strings (which you did, i’m pretty sure)
but at the wrong place
*/
return true;
}
else{
return false;
}
// Only change code above this line
}

console.log(isEveryoneHere(users))

I hope it’s not to much (i tried to explain what you did and how you can improve) as good as i could, without giving you to many hints, so you can figure it out yourself.
changing just what i mentioned the function isEveryoneHere(obj) now works as intended, though be aware that the result of it is not really what you want for the challenge.
Changing one of the users(e.g"Jeff") “online” property to false would still make your function return the same as before, if you know what i mean.
(It’s only halfway finished i’m sure the other half won’t be a problem for you)
I hope i could help you :smiley:
(i try to improve my coding by describing/explaining scripts from others, sorry if my explanation is over detalized )

1 Like

the names of the required keys are known: Alan, Jeff, Sara, Ryan

hasOwnProperty() only accepts one argument as a string. So you can only check one name at a time.

Method — Check the name of all the keys in the object for a match

  • If a key matches any of the required names, continue
  • If all keys match the required names, return true
  • If any key doesnt match required name, return false
  • If any key is missing, return false
1 Like