function checkObj(obj, checkProp) {
// Only change code below this line
var obj = {
gift: "pony",
pet: "kitten",
bed: "sleigh",
city: "Seattle",
};
if (obj.hasOwnProperty(checkProp)){
return obj[checkProp];
}
else {
return "Not Found";
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Testing Objects for Properties
the instructions are
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" .
Failed: checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") should return the string pony.
Failed:checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet") should return the string kitten.
Passed:checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house") should return the string Not Found.
Failed:checkObj({city: "Seattle"}, "city") should return the string Seattle.
Passed:checkObj({city: "Seattle"}, "district") should return the string Not Found.
Passed:checkObj({pet: "kitten", bed: "sleigh"}, "gift") should return the string Not Found.
Most people get tripped on this problem.
As mentioned earlier, you are not supposed to hardcode your own object here
The goal of this lesson is to teach you how to work with functions and have any object that is passed into the function. Not one that you hardcoded that just handles the test cases.
Right now, your function only works for this object you created here.
But what if I wanted to add more test cases and test this function with 100 different objects.
We don’t want to be in a situation where we have to hardcode 100 different objects or keep adding more properties to an existing object just to pass the tests.
I should be able to pass in any object I want to with your function and have the function return the correct output.
For example, try testing out this function call with your current code.
You should see an output of “Not Found” .
But that is not correct because there is key called “type” here type: "education platform" when we call the function.
Hopefully that clears up the confusion on why you shouldn’t hardcode your own object in this challenge.