2018-09-30 23:01:58 +01:00
---
id: 567af2437cbaa8c51670a16c
title: Testing Objects for Properties
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cm8Q7Ua'
2019-07-31 11:32:23 -07:00
forumTopicId: 18324
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
Sometimes it is useful to check if the property of a given object exists or not. We can use the < code > .hasOwnProperty(propname)< / code > method of objects to determine if that object has the given property name. < code > .hasOwnProperty()< / code > returns < code > true< / code > or < code > false< / code > if the property is found or not.
< strong > Example< / strong >
2019-05-17 06:20:30 -07:00
```js
var myObj = {
top: "hat",
bottom: "pants"
};
myObj.hasOwnProperty("top"); // true
myObj.hasOwnProperty("middle"); // false
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2020-03-24 22:09:57 -07:00
Modify the function < code > checkObj< / code > to test if an object passed to the function (< code > obj< / code > ) contains a specific property (< code > checkProp< / code > ). If the property is found, return that property's value. If not, return < code > "Not Found"< / code > .
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2020-03-24 22:09:57 -07:00
- text: '< code > checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")</ code > should return < code > "pony"</ code > .'
testString: 'assert(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") === "pony");'
- text: '< code > checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")</ code > should return < code > "kitten"</ code > .'
testString: 'assert(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet") === "kitten");'
- text: '< code > checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")</ code > should return < code > "Not Found"</ code > .'
testString: 'assert(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house") === "Not Found");'
- text: '< code > checkObj({city: "Seattle"}, "city")</ code > should return < code > "Seattle"</ code > .'
testString: 'assert(checkObj({city: "Seattle"}, "city") === "Seattle");'
- text: '< code > checkObj({city: "Seattle"}, "district")</ code > should return < code > "Not Found"</ code > .'
testString: 'assert(checkObj({city: "Seattle"}, "district") === "Not Found");'
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
2020-03-24 22:09:57 -07:00
function checkObj(obj, checkProp) {
2020-03-08 07:46:28 -07:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
return "Change Me!";
2020-03-08 07:46:28 -07:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
}
2020-03-24 22:09:57 -07:00
checkObj(myObj, "gift");
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
2020-03-24 22:09:57 -07:00
function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
2018-09-30 23:01:58 +01:00
} else {
return "Not Found";
}
}
```
< / section >