Basic JavaScript - Testing Objects for Properties

under this Testing Objects for Properties, this is my solution below, but it didn’t pass the test , what do I do next?

if(obj.hasOwnProperty(“checkProp”)) {
return obj[checkProp];
}else {
return “Not Found”
}

  **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"
}

return 
// 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/103.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

First all, you can see what it is returning by putting something like this at the bottom of your code:

console.log(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift"))

That will output to the console at the bottom.

So, why is it returning that? Here:

obj.hasOwnProperty("checkProp")

By putting checkProp in quotes, you are checking if it has a specific property named “checkProp”. You don’t want to check that, you want to check for the string contained in that variable. You want the variable there, not a string literal. Does that makes sense? You referenced the variable correctly here:

  return obj[checkProp];

There you didn’t put quotes around it because you (correctly) didn’t want a prop specifically named “checkProp” but one with a name that matches what is in that variable.

2 Likes

if(obj.hasOwnProperty(“checkProp”)) { return obj[checkProp]; this line the function CheckProp is not a string… delete that

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.