I need help on this code please

This is not working for me. someone please help

function checkObj(obj, 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
}
  **Your browser information:**

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

Challenge: Testing Objects for Properties

Link to the challenge:

I’ve edited your post for readability. 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 (’).

You have two problems.

First, you declare an object called “obj”:

var obj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

But the instructions say:

… test if an object passed to the function ( obj )…

So, you are not supposed to create your own object, but use the one passed into the function:

function checkObj(obj, checkProp) {

Your second problem is here:

if (obj.hasOwnProperty("checkProp")){

You are passing that method a string literal, so it is checking if that specific string exists, in other words, you are checking if obj has a property specifically called “checkProp”. But you want to check the string that is held in a variable by that name.

When I fix those two things, your code passes for me.

Thaanks a lot! it worked

1 Like

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