Testing Objects for Properties (again)

What can be added in the code below, so that

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") should return the string pony .

and in the same time,

checkObj({pet: "kitten", bed: "sleigh"}, "gift") should return the string Not Found .

*It sounds a bit counter-intuitive to me to meet both of those requirements, by manipulating only the interior of the function*

function checkObj(obj, checkProp) {
var obj = {
gift: "pony",
pet: "kitten",
bed: "sleigh",
city: "Seattle"
};
if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];} 
else {
return "Not Found";};
}
  **Your browser information:**

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

Challenge: Testing Objects for Properties

Link to the challenge:

Let me ask you this. Why are you redefining the variable obj in the function body? Remember, you want to check the object being passed into the function, which is the first argument (obj). If you redefine it in the function body then you lose the ability to check it.

1 Like

Done :). It was that easy. Thanks a lot, but it seems like it will take some time till the concepts settle down.

Any and all help would be appreciated…

I have been unable to solve the “Testing Objects for Properties” Basic JavaScript problem.

something wrong with my solution and all of the solution I find here, none actually work for me.

Here is my solution:]
function checkObj(obj, checkProp) {

// Only change code below this line

var checkObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”,
city: “Seattle”
};

checkObj.hasOwnProperty(“gift”);
checkObj.hasOwnProperty(“pet”);
checkObj.hasOwnProperty(“sleigh”);
checkObj.hasOwnProperty(“city”)

if (checkObj.hasOwnProperty(checkProp)) {
return checkObj[checkProp];
} else {
return “NotFound”;
}

// Test your code by modifying these values
checkObj({gift: “pony”, pet: “kitten”, bed: “sleigh”}, “gift”)
// Only change code above this line
}

HERE ARE MY ERRORS:
// running tests
checkObj({gift: “pony”, pet: “kitten”, bed: “sleigh”}, “house”) should return the string Not Found.

checkObj({city: “Seattle”}, “district”) should return the string Not Found.

checkObj({pet: “kitten”, bed: “sleigh”}, “gift”) should return the string Not Found.
// tests completed

This is the error in your code too.

i have modified as instructed in the video by replacing checkObj with obj but it still does not pass?

if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return “NotFound”;

}

… Are you still defining the object inside of your function?

here is my code. i believe that i am matching the example given as well as the video.

var checkObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”,
city: “Seattle”
};

checkObj.hasOwnProperty(“gift”);
checkObj.hasOwnProperty(“pet”);
checkObj.hasOwnProperty(“sleigh”);
checkObj.hasOwnProperty(“city”)

if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return “NotFound”;
}