.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
access-an-arrays-contents-using-bracket-notation.md
access-property-names-with-bracket-notation.md
add-items-to-an-array-with-push-and-unshift.md
add-items-using-splice.md
add-key-value-pairs-to-javascript-objects.md
check-for-the-presence-of-an-element-with-indexof.md
check-if-an-object-has-a-property.md
combine-arrays-with-the-spread-operator.md
copy-an-array-with-the-spread-operator.md
copy-array-items-using-slice.md
create-complex-multi-dimensional-arrays.md
generate-an-array-of-all-object-keys-with-object.keys.md
iterate-through-all-an-arrays-items-using-for-loops.md
iterate-through-the-keys-of-an-object-with-a-for...in-statement.md
modify-an-array-stored-in-an-object.md
modify-an-object-nested-within-an-object.md
remove-items-from-an-array-with-pop-and-shift.md
remove-items-using-splice.md
use-an-array-to-store-a-collection-of-data.md
use-the-delete-keyword-to-remove-object-properties.md
basic-javascript
basic-javascript-rpg-game
debugging
es6
functional-programming
functional-programming-spreadsheet
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
portuguese
russian
spanish
schema
test
.babelrc
LICENSE.md
comment-dictionary.js
create-challenge-bundle.js
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
crowdin.yml
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
122 lines
3.6 KiB
Markdown
122 lines
3.6 KiB
Markdown
---
|
|
id: 587d7b7d367417b2b2512b1c
|
|
title: Check if an Object has a Property
|
|
challengeType: 1
|
|
forumTopicId: 301155
|
|
---
|
|
|
|
## Description
|
|
<section id='description'>
|
|
Now we can add, modify, and remove keys from objects. But what if we just wanted to know if an object has a specific property? JavaScript provides us with two different ways to do this. One uses the <code>hasOwnProperty()</code> method and the other uses the <code>in</code> keyword. If we have an object <code>users</code> with a property of <code>Alan</code>, we could check for its presence in either of the following ways:
|
|
|
|
```js
|
|
users.hasOwnProperty('Alan');
|
|
'Alan' in users;
|
|
// both return true
|
|
```
|
|
|
|
</section>
|
|
|
|
## Instructions
|
|
<section id='instructions'>
|
|
We've created an object, <code>users</code>, with some users in it and a function <code>isEveryoneHere</code>, which we pass the <code>users</code> object to as an argument. Finish writing this function so that it returns <code>true</code> only if the <code>users</code> object contains all four names, <code>Alan</code>, <code>Jeff</code>, <code>Sarah</code>, and <code>Ryan</code>, as keys, and <code>false</code> otherwise.
|
|
</section>
|
|
|
|
## Tests
|
|
<section id='tests'>
|
|
|
|
```yml
|
|
tests:
|
|
- text: The <code>users</code> object should only contain 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);
|
|
- text: The function <code>isEveryoneHere</code> should return <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);
|
|
- text: The function <code>isEveryoneHere</code> should return <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);
|
|
- text: The function <code>isEveryoneHere</code> should return <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);
|
|
- text: The function <code>isEveryoneHere</code> should return <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);
|
|
- text: The function <code>isEveryoneHere</code> should return <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);
|
|
```
|
|
|
|
</section>
|
|
|
|
## Challenge Seed
|
|
<section id='challengeSeed'>
|
|
|
|
<div id='js-seed'>
|
|
|
|
```js
|
|
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
|
|
|
|
// Only change code above this line
|
|
}
|
|
|
|
console.log(isEveryoneHere(users));
|
|
```
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
## Solution
|
|
<section id='solution'>
|
|
|
|
```js
|
|
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));
|
|
```
|
|
|
|
</section>
|