Sure about solution but doesn't work

Problem with Solving this exercise:
I think this the correct solution but the it’s not accepted . what is my code’s problem?

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

**


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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36

Challenge: Testing Objects for Properties

Link to the challenge:

The parameter checkProp holds a string value. You should use that, not the string literal "checkProp".

1 Like

To expand on what’s happening.

Imagine you have an object with the following content:

const obj = {
  name: "Daphne"
  age: 20
};

And then we have a variable with the string “name”:

const prop = "name";

Then all of the following log “Daphne”:

console.log(obj.name) // "Daphne"
console.log(obj[prop]) // "Daphne"
console.log(obj["name"]) // "Daphne"
1 Like

Imagine instead that that prop variable can be mutated:

let prop = "name";
console.log(obj[prop]); // "Daphne"
prop = "age";
console.log(obj[prop]); // 20
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
}

return obj[checkProp]
Using it as ‘checkProp’ is returning the object with the string literal ‘checkProp’

1 Like

yeah understood my mistake . maybe I just need to take a little break :sweat_smile: tnx btw :pray:

That was nice explanation . thanks .

I have added [spoiler] tags around your code since this is a working solution.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thanks for your notice , I’m quite new in forum and still need to pay attention more to details!