This includes certificates (where it does nothing), but does not include any translations.
2.5 KiB
2.5 KiB
id, title, challengeType, isHidden, videoUrl, forumTopicId
id | title | challengeType | isHidden | videoUrl | forumTopicId |
---|---|---|---|---|---|
567af2437cbaa8c51670a16c | Testing Objects for Properties | 1 | false | https://scrimba.com/c/c6Wz4ySr | 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 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"
.
Tests
tests:
- 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");'
Challenge Seed
function checkObj(obj, checkProp) {
// Only change code below this line
return "Change Me!";
// Only change code above this line
}
Solution
function checkObj(obj, checkProp) {
if(obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return "Not Found";
}
}