Tell us what’s happening:
Describe your issue in detail here.
**Your code so far**
function checkObj(obj, checkProp) {
// Only change code below this line
var checkObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh",
city: "Seattle"
}
return obj[checkProp];
return "Not Found";
// Only change code above this line
}
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15
You don’t have to define any exact prop values here. Instead, you just want to check if obj has own property checkProp, and if it has then return the value of the prop, else return not found.
→ Write a function (checkObj)
→ The function will take an object. We don’t know what object, but we’re giving the generic name “obj” to whatever object gets passed in.
→ It will also take a property. Again, we don’t know what property, but the name we’re giving within the function is “checkProp”.
→ It will check to see if the object we passed in (obj) has the property (checkProp) that was passed in. For example, when we later run the function, we might run it as:
checkObj(cat, "meow")
In this case, cat takes on the role of obj and “meow” takes on checkProp. The function will check and see if the cat object has the “meow” property.
So, to sort of talk through what you want to write here:
function checkObj(obj, checkProp) {
// if obj has the property checkProp, return the property
// otherwise, return "Not Found"
}
Hopefully this helps you get started without giving anything away!
The return statement ends function execution and specifies a value to be returned to the function caller.
in your code there is two return statments.
your code is correct , but it only checks properties of an object which is defined inside the function body.
the second return statement will not work here, its not a if () {} else {} statement its function. Insted of doing this you have to check properties for an object which you don’t know. now question is how do that
this is object method object.hasOwnProperty(propertyName) check what it does.