# freeCodeCamp Challenge Guide: Testing Objects for Properties

its good, but u missed the learning point of the task.

You are right my solution is better:
// Setup
var myObj = {
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];
}
else{
}
}

// Test your code by modifying these values

My take:

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

function checkObj(checkProp) {

myObj.hasOwnProperty();

output = “pony”;
}else if(checkProp === “pet”) {
output = “kitten”;
}else if(checkProp === “bed”) {
output = “sleigh”;
}else{
}

return output;
}

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

1 Like

Yet another approach:

//Setup

var myObj = {
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
switch(checkProp) {

output = “pony”;
break;

case “pet”:
output = “kitten”;
break;

case “bed”:
output = “sleigh”;
break;

default:
break;

}
return output;

}

//Test your code by modifying these values

checkObj(“house”);

I don’t know why it won’t take my solution, even if the tests all come out right. This is how I solved it:

function checkObj(checkProp) {
if (myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
}
}

1 Like

Never mind, just realized the problem was the “!” at the end of Not Found; took me some time to figure it out haha.

1 Like

This fully works
// Setup
var myObj = {
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
return “pony”;
}else if(checkProp == “pet”){
return “kitten”;
}else if(checkProp == “house”){
}

return “Change Me!”;
}

// Test your code by modifying these values

Can anyone explain the difference between checkObj and checkProp? Is checkProp the specific wording that is always used when trying to see if a list contains something? Or is it just a variable made up for this example?

1 Like

Glad everyone got mostly the same answer… I failed this one miserably…

1 Like

A reminder to all to uncomment first the above line of codes so that the other lines after it will be able to run and read `myObj` properties.

Here is my codes,it’s also work.

var myObj = {
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp)==1){
return myObj[checkProp];
}
else{
}

}

// Test your code by modifying these values

Im not sure what im missing but, this is not working
// Setup
var myObj = {
pet: “kitten”,
bed: “sleigh”
};

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

// Test your code by modifying these values

lol changed if (myObj.hasOwnProperty(“checkProp”)){ to if (myObj.hasOwnProperty(checkProp)){

** THIS CODE WORKS **

function checkObj(checkProp) {
var result ="";
if (myObj.hasOwnProperty(checkProp)){
result= myObj[checkProp];
}else {
}

return result;
}

=============================
** THIS CODE DOES NOT - The else does not work BUTreturns the proper answer IF the value is found, but empty if not.

function checkObj(checkProp) {
var result ="";
if (myObj.hasOwnProperty){
result= myObj[checkProp];
}else {
}

return result;
}

1 Like

Not the most elegant solution, but this is how I did it.

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

function checkObj(checkProp) {
myObj.hasOwnProperty(checkProp);

var value = “”;

var pValue = myObj[checkProp];

value = myObj.hasOwnProperty(checkProp);
if (value == 1)
{
return pValue;
}
else
{
}

//return “Change Me!”;
}

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

//Here is my solution that can use for another object
// Setup
var myObj = {
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
var result;
var obj = myObj;
var testObj = myObj.hasOwnProperty(checkProp);
if(testObj){
result = obj[checkProp];
} else {
}
return result;
}

// Test your code by modifying these values

My solution:

if (myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
}

I see that everyone seems to be using and else statement to return “Not Found”, am I wrong in thinking this is redundant? This is my code, it works fine without an else statement.

var myObj = {
pet: “kitten”,
bed: “sleigh”
};
function checkObj(checkProp) {
if (myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];
}
}

Thanks Axel, a clear and elegant code
function checkObj(checkProp) {
}

1 Like

I did this and it worked, why?

var myObj = {
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {