I have problem with check if an Object has a Property

Tell us what’s happening:
Hello ! I can’t figure out why this code in comment didn’t work, and I don’t even know why the solution given below is not working. Help please

  **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 ('Alan' in users) {
  return true;
} else if ('Jeff' in users) {
  return true;
} else if ('Sarah' in users) {
  return true;
} else if ('Ryan' in users) {
  return true;
} else {
  return false;
} */  
if (users.hasOwnProperty('Alan', 'Jeff', 'Sarah', 'Ryan')) {
  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/90.0.4430.72 Safari/537.36.

Challenge: Check if an Object has a Property

Link to the challenge:

This won’t work because you can’t test multiple properties at once like this.

This won’t work because your function will immediately return the very first time it encounters a return statement.

I suspect you are supposed to use the function argument here.

Edit: Huh, it appears not. The function works with both obj and users used internally. Not sure what’s going on with that.

1 Like

for the commented code, it doens’t work because you only check for one name then return true if its a property of the object.

first the return statement stops the function from executing and return a value, so if the object passed looks for example like this:

Jeff: {
  age: 32,
  online: true
},
Sarah: {
  age: 48,
  online: true
},
Ryan: {
  age: 19,
  online: true
}
}

then your code will check for “Alan”, it will not find it then it goes to Jeff, it finds Jeff and returns true which stops the execution of the function.

the second part doesn’t work because object.hasOwnProperty takes only one argument, so it only evaluates the first argument passed to it.

as a hint to the solution, you want to return all those booleans at once so you need to “aggregate” them, there is an operator for that use it.

1 Like

Thank you ! I see what you mean

Thank you very much, that was helpful.

Not that I’d use this for this challenge, but I just wanted to add to the above answers - if you ever find yourself writing an if else statement that only returns a boolean, you can skip the if else and go straight to return for cleaner code. E.g

if ('Ryan' in users) {
  return true
} else {
  return false
}

is the same as:

return 'Ryan' in users
1 Like

Your right, thank you so much ! I just did what you said and it worked.
@jonohewitt @JeremyLT @nzz.coding thank you guys, I passed this challenge

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.