* fix: consolidate comments Co-authored-by: Parth Parth <34807532+thecodingaviator@users.noreply.github.com>
2.0 KiB
2.0 KiB
id, title, challengeType, videoUrl, forumTopicId
id | title | challengeType | videoUrl | forumTopicId |
---|---|---|---|---|
567af2437cbaa8c51670a16c | Testing Objects for Properties | 1 | https://scrimba.com/c/cm8Q7Ua | 18324 |
Description
.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
var myObj = {
top: "hat",
bottom: "pants"
};
myObj.hasOwnProperty("top"); // true
myObj.hasOwnProperty("middle"); // false
Instructions
checkObj
to test myObj
for checkProp
. If the property is found, return that property's value. If not, return "Not Found"
.
Tests
tests:
- text: <code>checkObj("gift")</code> should return <code>"pony"</code>.
testString: assert(checkObj("gift") === "pony");
- text: <code>checkObj("pet")</code> should return <code>"kitten"</code>.
testString: assert(checkObj("pet") === "kitten");
- text: <code>checkObj("house")</code> should return <code>"Not Found"</code>.
testString: assert(checkObj("house") === "Not Found");
Challenge Seed
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// Only change code below this line
return "Change Me!";
// Only change code above this line
}
checkObj("gift");
Solution
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
} else {
return "Not Found";
}
}