Why I am getting this type of example and everybody is changing code above "this line"?

Tell us what’s happening:

function checkObj(obj, checkProp) {

// Only change code below this line

return “Change Me!”;

// Only change code above this line

}

Your code so far


function checkObj(obj, checkProp) {
// Only change code below this line
return "Change Me!";
// Only change code above this line
}

Your browser information:

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

Challenge: Testing Objects for Properties

Link to the challenge:

Welcome, dhiraj.

Could you elaborate on what you are having trouble with?

I am copying this code from Basic Javascript topic “Testing Object from properties”. Problem is this that in example different code is showing and in example video different.
Though we cannot change code above the mention line which is totally neglected in the video example.

The video is there to help you understand the concepts. It is not intended that you will exactly duplicate the video character for character to solve the challenge.

the video is for an old version of the challenge, but if you understand the topic you should be able to solve also this version

1 Like

Ignore the video, it uses an old version of this challenge. I have confidence you can do this without having a video.

The function definition you are seeing is correct. Do not change anything other than adding code between the two comments.

1 Like

OK got it but I am unable to figure out answer and when I check for hint everyone pasting same answer but I want answer related to example because I tried for half an hour myself but didn’t get answer.

What is your code? You can ask for help here on the forum.

what have you tried? what do you not know how to do?

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

}

console.log(checkObj(gift));

I am getting gift not defined

This is because gift is not a defined variable. You want to use "gift".

Could you please put ``` on the lines before and after your code so that it is more readable?

You will want to remove the

var obj = {

gift : "pony",

pet : "kitten",

bed : "sleigh"

};

portion of your code. This function should work for any input object, not just that one specific object from the video.

After “gift” it is showing same error.

I am not sure why. I copied and pasted your code into repl.it and made only the change "gift" and your code ran.

Ah, you are not using the correct function signature, which is going to cause trouble as well.

You have several issues:

  1. Use "property_name" (i.e. use "gift")
  2. You need to use the correct function signature: checkObj(obj, checkProp)
  3. You need to remove the definition of an object inside of your function. This is overwriting the parameter passed in.
  4. return obj.checkProp will not return anything. You want to use the other notation to access your property (think about why…)
1 Like

Can you give me example?

I edited my reply above.

Should I have to declare var=obj outside the function for correct function signature?


  // 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

}

console.log(checkObj(gift));```

You will want to totally delete the definition of the object for the code you submit.
You will also need to remove the console log.

What I mean by the function signature is that you are calling checkObj("gift") but you should be calling checkObj(obj, "gift").

Really, you have a lot going on here that you don’t want. I’d reset the challenge and add only the lines of code that include the logic for checking for the property and returning the correct value.

Can you give me solution please? Related to this example?