99 lines
2.3 KiB
Markdown
99 lines
2.3 KiB
Markdown
![]() |
---
|
||
|
id: 567af2437cbaa8c51670a16c
|
||
|
title: Testing Objects for Properties
|
||
|
challengeType: 1
|
||
|
videoUrl: 'https://scrimba.com/c/c6Wz4ySr'
|
||
|
forumTopicId: 18324
|
||
|
dashedName: testing-objects-for-properties
|
||
|
---
|
||
|
|
||
|
# --description--
|
||
|
|
||
|
Sometimes it is useful to check if the property of a given object exists or not. We can use the `.hasOwnProperty(propname)` method of objects to determine if that object has the given property name. `.hasOwnProperty()` returns `true` or `false` if the property is found or not.
|
||
|
|
||
|
**Example**
|
||
|
|
||
|
```js
|
||
|
var myObj = {
|
||
|
top: "hat",
|
||
|
bottom: "pants"
|
||
|
};
|
||
|
myObj.hasOwnProperty("top");
|
||
|
myObj.hasOwnProperty("middle");
|
||
|
```
|
||
|
|
||
|
The first `hasOwnProperty` returns `true`, while the second returns `false`.
|
||
|
|
||
|
# --instructions--
|
||
|
|
||
|
Modify the function `checkObj` to test if an object passed to the function (`obj`) contains a specific property (`checkProp`). If the property is found, return that property's value. If not, return `"Not Found"`.
|
||
|
|
||
|
# --hints--
|
||
|
|
||
|
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")` should return the string `pony`.
|
||
|
|
||
|
```js
|
||
|
assert(
|
||
|
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'gift') === 'pony'
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")` should return the string `kitten`.
|
||
|
|
||
|
```js
|
||
|
assert(
|
||
|
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'pet') === 'kitten'
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")` should return the string `Not Found`.
|
||
|
|
||
|
```js
|
||
|
assert(
|
||
|
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'house') ===
|
||
|
'Not Found'
|
||
|
);
|
||
|
```
|
||
|
|
||
|
`checkObj({city: "Seattle"}, "city")` should return the string `Seattle`.
|
||
|
|
||
|
```js
|
||
|
assert(checkObj({ city: 'Seattle' }, 'city') === 'Seattle');
|
||
|
```
|
||
|
|
||
|
`checkObj({city: "Seattle"}, "district")` should return the string `Not Found`.
|
||
|
|
||
|
```js
|
||
|
assert(checkObj({ city: 'Seattle' }, 'district') === 'Not Found');
|
||
|
```
|
||
|
|
||
|
`checkObj({pet: "kitten", bed: "sleigh"}, "gift")` should return the string `Not Found`.
|
||
|
|
||
|
```js
|
||
|
assert(checkObj({ pet: 'kitten', bed: 'sleigh' }, 'gift') === 'Not Found');
|
||
|
```
|
||
|
|
||
|
# --seed--
|
||
|
|
||
|
## --seed-contents--
|
||
|
|
||
|
```js
|
||
|
function checkObj(obj, checkProp) {
|
||
|
// Only change code below this line
|
||
|
return "Change Me!";
|
||
|
// Only change code above this line
|
||
|
}
|
||
|
```
|
||
|
|
||
|
# --solutions--
|
||
|
|
||
|
```js
|
||
|
function checkObj(obj, checkProp) {
|
||
|
if(obj.hasOwnProperty(checkProp)) {
|
||
|
return obj[checkProp];
|
||
|
} else {
|
||
|
return "Not Found";
|
||
|
}
|
||
|
}
|
||
|
```
|