Testing objects for properties exercise

Can someone take a look at my code? I’ve been struggling for a while now and can’t figure out why it won’t run.

// Setup

var myObj = {

gift: “pony”,

pet: “kitten”,

bed: “sleigh”

};

function checkObj(checkProp) {

// Your Code Here

if (myObj.hasOwnProperty(checkProp)) {

return myObj[checkProp];

} else {

return "Not Found"

}

}

You’ll need to hit the “Reset All Code” button. It looks like you’re using outdated starter code, or watching the help video which is using outdated code.

The new problem defines the function with an object parameter obj, as per the instructions:

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

1 Like

I believe there is a problem with this lesson. The only starter code we are given is:

function checkObj(obj, checkProp) {

// Only change code below this line

return “Change Me!”;

// Only change code above this line

}

There is not a problem with this lesson. The starter code is correct.

The purpose of the videos is not for you to copy them character for character. They are intended to discuss the ideas with you and help you understand the challenge. The written text of the challenge and the starter code are still the requirements that you have to meet.

In this case, the video makes use of a global variable in the function, which is not a best practice. The challenge wants you to do the same thing as the video, but with the passed in object instead of using the global variable for the object.

That is the correct code. The challenge has been updated since the last time someone made a video. I recommend being extremely conservative with the videos. They can be a useful tool to see how people solve problems, but people tend to turn their brains off and just copy the code they see without thinking critically. In this case, it meant you completely ignored the instruction that said “Don’t change anything above this line”.

Personally, I hate that the videos are there and I wish that freeCodeCamp would get rid of them.

1 Like

Understood. And I typically just use the video as a learning tool if I’m stuck.

I just really don’t understand this lesson. Am I supposed to create an (obj) function and add properties? I’m new to coding. Where can I get more help?

You are supposed to take the obj and checkProp and see if the object obj has the property checkProp. We can help you with any questions you have along the way.

No. Both obj and checkProp are arguments that are passed into the function. In that way it’s not any different than functions you have already done.

// a function that takes two numbers, a and b, and returns their sum
function addTogether(a, b) {
    return a + b;
}