2018-09-30 22:01:58 +00:00
---
id: 567af2437cbaa8c51670a16c
title: Testing Objects for Properties
challengeType: 1
2019-07-31 18:32:23 +00:00
forumTopicId: 18324
2021-01-13 02:31:00 +00:00
dashedName: testing-objects-for-properties
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --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**
2019-05-17 13:20:30 +00:00
```js
2021-10-25 16:55:58 +00:00
const myObj = {
2019-05-17 13:20:30 +00:00
top: "hat",
bottom: "pants"
};
2021-10-25 16:55:58 +00:00
2021-03-03 00:12:12 +00:00
myObj.hasOwnProperty("top");
myObj.hasOwnProperty("middle");
2019-05-17 13:20:30 +00:00
```
2021-03-03 00:12:12 +00:00
The first `hasOwnProperty` returns `true` , while the second returns `false` .
2020-11-27 18:02:05 +00:00
# --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--
2021-03-03 00:12:12 +00:00
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")` should return the string `pony` .
2020-11-27 18:02:05 +00:00
```js
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'gift') === 'pony'
);
```
2021-03-03 00:12:12 +00:00
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")` should return the string `kitten` .
2020-11-27 18:02:05 +00:00
```js
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'pet') === 'kitten'
);
```
2021-03-03 00:12:12 +00:00
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")` should return the string `Not Found` .
2020-11-27 18:02:05 +00:00
```js
assert(
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'house') ===
'Not Found'
);
```
2021-03-03 00:12:12 +00:00
`checkObj({city: "Seattle"}, "city")` should return the string `Seattle` .
2020-11-27 18:02:05 +00:00
```js
assert(checkObj({ city: 'Seattle' }, 'city') === 'Seattle');
2018-09-30 22:01:58 +00:00
```
2021-03-03 00:12:12 +00:00
`checkObj({city: "Seattle"}, "district")` should return the string `Not Found` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(checkObj({ city: 'Seattle' }, 'district') === 'Not Found');
```
2018-09-30 22:01:58 +00:00
2021-03-03 00:12:12 +00:00
`checkObj({pet: "kitten", bed: "sleigh"}, "gift")` should return the string `Not Found` .
2020-11-27 18:02:05 +00:00
```js
assert(checkObj({ pet: 'kitten', bed: 'sleigh' }, 'gift') === 'Not Found');
```
# --seed--
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2020-03-25 05:09:57 +00:00
function checkObj(obj, checkProp) {
2020-03-08 14:46:28 +00:00
// Only change code below this line
2018-09-30 22:01:58 +00:00
return "Change Me!";
2020-03-08 14:46:28 +00:00
// Only change code above this line
2018-09-30 22:01:58 +00:00
}
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2020-03-25 05:09:57 +00:00
function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
2018-09-30 22:01:58 +00:00
} else {
return "Not Found";
}
}
```