It seems the practices are getting complicated

Tell us what’s happening:

I’ve tried different methods but none of them seemed to be right. I’m getting confused. please help me.

Your code so far



function checkObj(myObj, checkProp){
var myObj = {
   gift: "pony",
   pet: "kitten",
   city: "Seattle"
 };
 myObj.hasOwnProperty("gift");
 myObj.hasOwnProperty("pet");
 myObj.hasOwnProperty("city")

if (myobj.hasOwnProperty("checkProp")){
 return myobj[checkProp];
}
else{
 return "Not Found";
}
}
 // Only change code above this line
checkobj("");

Your browser information:

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

Challenge: Testing Objects for Properties

Link to the challenge:

You are close. You have two issues:

  1. You shouldn’t be declaring myObj inside of your function. Your function should work for any object.
  2. You shouldn’t be using "checkProp". The variable checkProp contains the string that you want to check for. I’d remove the quotation marks.
1 Like

main issue is that you are overwriting the function parameter

why have you declared myObj inside the function?

2 Likes

It is myObj not myobj. And as said you need to use the checkProp parameter, not the string “checkProp”.

You can technically pass the tests with the object inside the function, but it’s still wrong to have it.

// should be myObj, not myobj and checkProp not "checkProp"
if (myobj.hasOwnProperty("checkProp")) {
 return myobj[checkProp];
}

Tell us what’s happening:

I tried other ways too and everything else, it doesn’t give me even one thick. Is there anything I’ve been missing?

Your code so far




var myObj = {
   gift: "pony",
   pet: "kitten",
   bed: "sleigh",
   city: "Seattle"
 };

 // Only change code bellow this line
function checkObj(obj, checkprop){
result = "";
if (myobj.hasOwnProperty(checkprop)){
 return myobj.checkProp;
}
else{
 return "Not Found";
}
}

 // Only change code above this line
 checkObj = ("gift");

Your browser information:

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

Challenge: Testing Objects for Properties

Link to the challenge:

Let’s look at your function:

function checkObj(obj, checkprop) {
  // Are you using this variable?
  // If not, you should delete it.
  result = "";

  // Where did myobj come from? 
  // The function argument is obj!
  if (myobj.hasOwnProperty(checkprop)) {
   return myobj.checkProp;
  } else {
    return "Not Found";
  }
}

Now let’s look at your function call:

// Only change code above this line

// It looks like you changed the code down here!
// There is no = in a function call!
// I would reset the code before you submit the code.
// When you call this, you need two arguments, matching the function definition above.
checkObj = ("gift");
1 Like

I got it. Thanks a million . It worked. :wink: :raised_back_of_hand: :raised_back_of_hand: :+1:

1 Like