Tell us what’s happening:
Describe your issue in detail here.
So I already managed to complete this challenge by looking up some advice on some older forum posts, but I still have no idea why this code works nor could I find an understandable explanation.
Basically the example shown in the test was how to use .hasOwnProperty to show whether it was true or false if a given object had a property name or not.
Example Given:
(const myObj = {
top: “hat”,
bottom: “pants”
};
myObj.hasOwnProperty(“top”);
myObj.hasOwnProperty(“middle”);
The first hasOwnProperty returns true, while the second returns false.)
The test itself on the other hand involved using .hasOwnProperty on an argument inside of a function to test, as best as I can tell, for the presence of a second argument contained within the function?
So basically what I don’t understand is-
1: How does using .hasOwnProperty on a function, or possibly the argument inside of a function once again I’m not sure, relate to using it to search for a parameter inside of an object?
2: Why am I using obj.hasOwnProperty(checkProp), obj the argument, to check whether or not it is true the function checkObj contains an argument called checkProp. Shouldn’t I be using checkObj.hasOwnProperty(checkProp), since it is the function that contains the argument? And how does using .hasOwnProperty on an argument show that there is another specific argument contained within the same function?
If someone could please explain this to me that would be really nice.
Your code so far
function checkObj(obj, checkProp) {
// Only change code below this line
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Testing Objects for Properties
Link to the challenge: