Dot or braket notation?

Hey! I don’t know if that has been asked before.
I was doing this exercise:

and my solution was exactly the same as camperbot’s

except that I used dot.notation instead of braket[notation] on the third line. So I typed:

return obj.checkProp;

instead of:

return obj[checkProp];

and my solution was not accepted. I went over my code many times and I tried brakets out of curiosity and it worked. Why is that? Thank you!

6 Likes

Hello~!

Dot notation looks for a specific property called “checkProp”, where bracket notation looks for a property called the value of the “checkProp” variable.

20 Likes

If, I only want using dot notation, instead of bracket notation, is there a way to do that?

you need to not use variables

It took me five minutes to figure out that obj.checkProp would return undefined as it was looking for the the value “checkProp” in obj that does not exist.
return obj[checkProp]; uses the value stored inside checkProp so it would actually look for obj.gift and return pony etc.

i find console.log(variable in here); very useful to find the issues in my code :slight_smile:

1 Like
var myobj = {gift: "pony", pet: "kitten", bed: "sleigh"}
function checkObj(myobj, checkProp) {
 var result ="";
 if (myobj.hasOwnProperty(checkProp))
 {
 result = myobj.checkProp;
 console.log("Found");
return result ;}
else {
return "Not Found";}}
checkObj(myobj, "gift")

both Dot and bracket notation worked for me

Hi @swatyyy !

Welcome to the forum!

You not supposed to create your own object here

The goal is to create a function that works for any object when it is called.
It shouldn’t just work for an object that you hardcoded yourself.

You need to change this function parameter back to obj instead of myobj.

Hope that makes sense!

oh ok… Thank you for explaining… Now I’m stuck :slight_smile:

If you need help with a challenge, I would suggest using the Ask for Help button which will create a new topic with your code and challenge link.