Can someone please explain?

I don’t know why it’s not working.
At first I thought it was bcz I created a local variable instead of a global variable. But it still doesn’t work with a global variable. I tried different ways to solve it, but nothing seems to work. Maybe I’m just not understanding the question.
Could someone please explain me the whole thing?

  **My code so far**

function checkObj(checkProp) {
// Only change code below this line
var obj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

if (obj.hasOwnProperty(checkProp)){
  return obj[checkProp];
}else{
  return "Not Found";
}

// Only change code above this line
}

Hi @sysilvy21 !

I edited your post to include the challenge link.

I would reset the lesson because there are two issues here.

No.1:
The function needs two parameters.
Not one.

This is incorrect.

When you reset the lesson it should look like this

function checkObj(obj, checkProp)

No.2:
You are not supposed to create your own object.

This is incorrect.

You are supposed to create a function that works for ANY object.
Not just one that you hardcoded yourself.

Hope that helps!

1 Like

It’s still not working :frowning_face_with_open_mouth: . I don’t understand why.
You said I need to create a function. Does that mean that the function needs to be created inside of the checkObj() ?
Honestly I’m still confused with the question. The question was like, " Modify the function checkObj to test if an object passed to the function ( obj ) contains a specific property ( checkProp )." Why does it say " the function ( obj ) contains a specific property ( checkProp )" ? Does this mean the new function name will be the property name of the function checkObj() ?
Please explain this.
And here’s my modified code.

Thanks!

Right now you have a function inside of a function.

I would reset the lesson again.

The only thing you want to add is the if else statement.

That’s it

Also, please write code directly into the forum instead of posting screenshots.

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 (’).

1 Like

You didn’t need to create a function.

In your original code you had deleted one of the parameters.

I just wanted you to reset the lesson so you can use the correct function definition with both parameters.

Hopefully that is clearer now.

It worked now! You were right, all I had to do was to add the if/else statement and I made it so complex.
Here’s my working code.

function checkObj(obj, checkProp) {
  // Only change code below this line
  if (obj.hasOwnProperty(checkProp)){
    return obj[checkProp];
  }else{
    return "Not Found";
  }
  return "Change Me!";
  // Only change code above this line
}

Thanks for your time and explanations! :blush:

1 Like

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