* Add test reflecting #19390 Added two tests to prevent checking only `Alan`. * Split into 4 tests for each prop Split previous tests with 2 props on each into 4 single-prop tests; fixed whitespace
4.5 KiB
4.5 KiB
id, title, challengeType
| id | title | challengeType |
|---|---|---|
| 587d7b7d367417b2b2512b1c | Check if an Object has a Property | 1 |
Description
hasOwnProperty() method and the other uses the in keyword. If we have an object users with a property of Alan, we could check for its presence in either of the following ways:
users.hasOwnProperty('Alan');
'Alan' in users;
// both return true
Instructions
users, with some users in it and a function isEveryoneHere, which we pass the users object to as an argument. Finish writing this function so that it returns true only if the users object contains all four names, Alan, Jeff, Sarah, and Ryan, as keys, and false otherwise.
Tests
tests:
- text: 'The <code>users</code> object only contains the keys <code>Alan</code>, <code>Jeff</code>, <code>Sarah</code>, and <code>Ryan</code>'
testString: 'assert("Alan" in users && "Jeff" in users && "Sarah" in users && "Ryan" in users && Object.keys(users).length === 4, "The <code>users</code> object only contains the keys <code>Alan</code>, <code>Jeff</code>, <code>Sarah</code>, and <code>Ryan</code>");'
- text: 'The function <code>isEveryoneHere</code> returns <code>true</code> if <code>Alan</code>, <code>Jeff</code>, <code>Sarah</code>, and <code>Ryan</code> are properties on the <code>users</code> object'
testString: 'assert(isEveryoneHere(users) === true, "The function <code>isEveryoneHere</code> returns <code>true</code> if <code>Alan</code>, <code>Jeff</code>, <code>Sarah</code>, and <code>Ryan</code> are properties on the <code>users</code> object");'
- text: 'The function <code>isEveryoneHere</code> returns <code>false</code> if <code>Alan</code> is not a property on the <code>users</code> object'
testString: 'assert((function() { delete users.Alan; return isEveryoneHere(users) })() === false, "The function <code>isEveryoneHere</code> returns <code>false</code> if <code>Alan</code> is not a property on the <code>users</code> object");'
- text: 'The function <code>isEveryoneHere</code> returns <code>false</code> if <code>Jeff</code> is not a property on the <code>users</code> object'
testString: 'assert((function() { delete users.Jeff; return isEveryoneHere(users) })() === false, "The function <code>isEveryoneHere</code> returns <code>false</code> if <code>Jeff</code> is not a property on the <code>users</code> object");'
- text: 'The function <code>isEveryoneHere</code> returns <code>false</code> if <code>Sarah</code> is not a property on the <code>users</code> object'
testString: 'assert((function() { delete users.Sarah; return isEveryoneHere(users) })() === false, "The function <code>isEveryoneHere</code> returns <code>false</code> if <code>Sarah</code> is not a property on the <code>users</code> object");'
- text: 'The function <code>isEveryoneHere</code> returns <code>false</code> if <code>Ryan</code> is not a property on the <code>users</code> object'
testString: 'assert((function() { delete users.Ryan; return isEveryoneHere(users) })() === false, "The function <code>isEveryoneHere</code> returns <code>false</code> if <code>Ryan</code> is not a property on the <code>users</code> object");'
Challenge Seed
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
// change code above this line
}
console.log(isEveryoneHere(users));
Solution
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) {
return [
'Alan',
'Jeff',
'Sarah',
'Ryan'
].every(i => obj.hasOwnProperty(i));
}
console.log(isEveryoneHere(users));