So what's the easiest way to check if an object has a property.
Exercise
Finish writing the function so that it returns true if the object passed to it contains all four names, Alan, Jeff, Sarah and Ryan and returns false otherwise.
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
function isEveryoneHere(userObj) {
// Only change code below this line
// Only change code above this line
}
Answer
function isEveryoneHere(userObj) {
// Only change code below this line
if (userObj.hasOwnProperty('Alan') &&
userObj.hasOwnProperty('Jeff') &&
userObj.hasOwnProperty('Sarah') &&
userObj.hasOwnProperty('Ryan'))
{
return true
}
else {
return false
}
// Only change code above this line
}
Top comments (0)