Tell us what’s happening:
Describe your issue in detail here.
I’m not sure about the requirements and how to do this exercise, can anyone help? result comes out at “false” sometimes, “not found” sometimes. at this moment, no matter what is put in console.log () returns false.
the original is function checkObj(obj, checkProp), but the request is
" 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"
."
I think this mean i have to use .hasOwnProperty to check if ‘checkProp’ is available within obj or not, not inside ‘checkObj’. Does that mean it is just a simple check on the properties and return certain answers like ‘Not found’ and the values instead of ‘true’ or ‘false’. It would be be so helpful if i can have a complete code with some explanations.
Your code so far
function checkObj(obj) {
// Only change code below this line
obj = {
gift: "pony",
pet: "kitten",
bed: "sleigh",
city: "Seattle"
};
if (obj.hasOwnProperty("")==true){return obj} else {return "Not found"}
// Only change code above this line
}
console.log(checkObj.hasOwnProperty("city"));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0
Challenge: Basic JavaScript - Testing Objects for Properties
Link to the challenge: