Testing Objects for Properties

I need help with this challenge:

Instructions
Modify the function checkObj to test myObj for checkProp. If the property is found, return that property’s value. If not, return “Not Found”.

This is what I have so far:

`// Setup
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp) === true) {
return myObj.checkProp;
} else {
return “Not Found”;
}

}

// Test your code by modifying these values
checkObj(“gift”);
`

3 Likes

return myObj.checkProp;

This would look for a property named checkProp in the Object. Could you try accessing the property by myObj[checkProp]

14 Likes

That was it. It seems so simple when someone else points it out to you. Thank you!

Had this exact same problem. Why does the checkProp not end up being a variable in the dot example but in the bracket example it ends up being a variable?

4 Likes

Another solution can be

function checkObj(checkProp) {
// Your Code Here

return myObj.hasOwnProperty(checkProp) ? myObj.checkProp: “Not Found”;
}

to make the code looks cleaner

3 Likes

Woah I did it completely different and passed the test… … I understand the challenge was to use the “.(propname)” I was confused on the way it was explained soooo I tinkered around and did it like this

return myObj[checkProp] || “Not Found”;

20 Likes

wow very simple CODE…
return myObj[checkProp] || “Not Found”;
so you use || as if false return "Not found"
vice versa…

I had the the same problem. I found these information about the square bracket access and the dot access.

“Square bracket notation allows access to properties containing special characters and selection of properties using variables”

4 Likes

Final code;

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};
function checkObj(checkProp) {
  // Your Code Here
  if (myObj.hasOwnProperty(checkProp) === true) {
    return myObj[checkProp];
  } else {
    return "Not Found";
  }
}
// Test your code by modifying these values
checkObj("gift");
9 Likes

Old post but, nice work!!

super smart way to solve this one. :clap:

I have a similar solution as you but I left out the === true since an if statement always results in a true or false statement.

// 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";
}

// Test your code by modifying these values
checkObj("gift");

1 Like

@Phillipe-Bojorquez You don’t even need to use the else keyword.

1 Like

Can anyone tell me why this doesn’t work?

function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(“checkProp”)) {
return myObj[checkProp];
}
return “Not Found”;
}

I have checkProp in quotes like so:

myObj.hasOwnProperty(“checkProp”)

I was following the example:

myObj.hasOwnProperty(“top”);

I don’t believe you need the “” around checkProp in the function.
try something like this

if (myObj.hasOwnProperty(checkProp)) {
also when you are returning the object just need to do this
return myObj[checkProp]

Hopefully this will help if you need the full code let me know

See if that helps

Can anyone explain why I need to use parentheses instead of brackets around checkProp in the second line of my code that is pasted below?

function checkObj(checkProp) {
if (myObj.hasOwnProperty(checkProp) === true) { <<<THIS LINE
return myObj[checkProp];
} else {
return “Not Found”;
}
}

Because hasOwnProperty is a function. When calling a function, you wrap any arguments being passed to the function with ( ).

2 Likes

// Setup
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
// Your Code Here
if(myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];
};
return “Not Found”;
}

// Test your code by modifying these values
checkObj(“pet”);

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, instead of posting a full solution, give the OP hints and suggestions unless the OP has already indicated a solution has been found.

Thank you.

I recently went through this challenge and I found out that this works in the console:

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

function checkObj(checkProp) {
  // Your Code Here
  
 if (myObj.hasOwnProperty(checkProp)) {
   return console.log(checkProp);
 } else {
   return console.log("Not Found");
 }
}

checkObj("bed"); // "sleigh"

But the same code w/o the console.log() does not work in the challenge… To make it work I had to use brackets in the first condition: myObj[checkProp]

Is this a bug or there’s something I’m missing here?