Error after inserting variable properties?

Hi, I understood the logic of the code for this challenge. My problem is knowing how to test it.

I looked back at my notes and I pasted the following above the function to define the variable properties. Others used this and said it worked:

var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

However, when I run it, Repl.it spits back the following error:
/home/runner/NutritiousNanoFeed/index.js:3
gift: “pony”,

SyntaxError: Invalid or unexpected token

The code was correct in the challenge, but I always want to try it in Repl.it to see it go. Can anyone please help me understand the error code and how I made a mistake in actually running this code in Repl.it? – Much appreciated! :slight_smile:

//Code so far: 
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

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
}

// Test your code by modifying these values
checkObj(“gift”);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

Not sure if you realize you have posted curly double quotes instead of straight double quotes. JavaScript only recognizes straight single and double quotes.

Also, there is no need put that object before the function. The function is called with various objects.

Thank you, that really helps!

Randell, fixing the quotes fixed the issue. But I left the object before the function and it worked. Where would be the better location? This is an important thing I don’t think I am understanding. I know the object must be defined but I’m still fumbling around with placement of them in the code.
Thank you again for your time. :slight_smile:

You do not need to declare an object anywhere. The function definition starts as:

function checkObj(obj, checkProp) {

Each of our tests will pass an object (represented by the obj parameter of the function). Once passed to the function, obj is just a local function variable. You can think of obj as a declaration and it is just awaiting some data to be passed to it for your use inside the function.

That makes sense. But I still have to put in

var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

inside the brackets or checkObj(“gift”); would not know where to look. Am I correct that you are saying it should be placed after the
return “Not Found”;
}

This is not needed as it is never used. Each test passes a different object (the first argument) and the property to look for (the second argument) to the function. Your function should be able to handle any object and property passed to it and return either the value of the property (if found) or the string “Not Found”. For example, if your function is written correctly, I should be able to make the following call to it:

checkObj({ name: "terribeary", student: "yes" }, "student") ;

and it should return "yes".

The object and property passed have absolutely nothing to do with the myObj you are wanting to put into your code. That is why you do not need it.

Thank you for your patience. I tested it and now completely understand why this works. :slight_smile: