Why is this solution wrong?

Can you elaborate on why my solution here is incorrect?

Thank you.

  **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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15

Challenge: Testing Objects for Properties

Link to the challenge:

Why do you have quotation marks here?

Here’s the challenge example ;

const myObj = {
  top: "hat",
  bottom: "pants"
};

myObj.hasOwnProperty("top");
myObj.hasOwnProperty("middle");

Ok. But what is checkProp?

Here’s what the challenge wants. Am I missing something?

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" .

Look at the example. "top" and "middle" are just some strings. But in the function, checkProp is a variable holding a string.

1 Like

“top” and “middle” are properties of the object myObj, not just some strings.

Property names are just strings. You are missing my point though. "top" and "middle" are strings while checkProp is a variable that holds a string. Do you put quotation marks around a variable to use it?

I’m just following what the exercise shows.

Also, according to this ;

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" .

“checkProp” is a property (of the object “obj”), not a standart “variable that holds a string”.

I have done this challenge dozens of times.

checkObj is a function. The checkObj function takes two arguments, obj and checkProp. The obj argument is a variable holding the object that you want to check. The checkProp argument is a variable holding the string with the property name you want to check for.

Your syntax is mostly right, but you are not treating checkProp like a variable.


const myProp = "this is some string property name";
console.log(myProp); // Notice no quotation marks here!

I didn’t use quotation marks around my myProp variable above when I used it inside of the console.log() function.

2 Likes

Hi @sanberk !

Welcome to the forum!

Maybe it would help to understand by looking at a function call.

Let’s take a look at this function call.

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

Let’s test this out with your current code here

if (obj.hasOwnProperty("checkProp")) {
  return obj.checkProp;
}

else {
  return "Not Found";
}

Right now your if condition is checking if "checkProp" is a property name in this object.

{gift: "pony", pet: "kitten", bed: "sleigh"}

We can see that "checkProp" is not one of the given properties.
The only properties we have here are gift, pet and bed.

However, that poses an issue because the test case tells use that this function call should return pony instead of Not Found.

We could fix that by refactoring our if statement to be the following.

if (obj.hasOwnProperty(checkProp))

Now if we test our code with the function call here

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

Remember that checkProp acts as a placeholder for the real values which in this function call would be “gift”.

Hopefully that makes sense.

The last thing we have to change is this part here

You can’t use dot notation here. We have to use bracket notation.
We should only use dot notation if we know the property’s name.

In this case, checkProp is a parameter and acts as a placeholder for the real value.
We can have dozens of function calls where the value of checkProp will be different each time depending on the values in the function call.

I really hope all of this makes sense.

You are really close to solving the challenge.
You just have to make those two changes to your code to pass.

Hope that helps!

2 Likes

This is the a good article on when to use dot notation versus bracket notation.

This was a great explanation. Thank you!

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