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 = {
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”);

My take:

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

function checkObj(checkProp) {
// Your Code Here

myObj.hasOwnProperty();

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

return output;
}

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

1 Like

Yet another approach:

//Setup

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

function checkObj(checkProp) {
//Your code here
switch(checkProp) {

case “gift”:
output = “pony”;
break;

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

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

default:
output = “Not Found”;
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) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp)) {
return myObj[checkProp];
}
return “Not Found!”;
}

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 :slight_smile:
// Setup
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
// Your Code Here
if(checkProp == “gift”){
return “pony”;
}else if(checkProp == “pet”){
return “kitten”;
}else if(checkProp == “house”){
return “Not Found”;
}

return “Change Me!”;
}

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

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 = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

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

}

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

Im not sure what im missing but, this is not working :confused:
// 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”);

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

** THIS CODE WORKS **

function checkObj(checkProp) {
// Your Code Here
var result ="";
if (myObj.hasOwnProperty(checkProp)){
result= myObj[checkProp];
}else {
result = “Not Found”;
}

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) {
// Your Code Here
var result ="";
if (myObj.hasOwnProperty){
result= myObj[checkProp];
}else {
result = “Not Found”;
}

return result;
}

1 Like

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

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

function checkObj(checkProp) {
// Your Code Here
myObj.hasOwnProperty(checkProp);

var value = “”;

var pValue = myObj[checkProp];

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

//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 = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

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

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

My solution:

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

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 = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};
function checkObj(checkProp) {
if (myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];
}
return “Not Found”;
}
checkObj(“gift”);

Thanks Axel, a clear and elegant code
function checkObj(checkProp) {
return myObj.hasOwnProperty(checkProp) === true ? myObj[checkProp] : “Not Found”;
}

1 Like

I did this and it worked, why?

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

function checkObj(checkProp) {
// Your Code Here

myObj.hasOwnProperty(checkProp);
return myObj[checkProp] || “Not Found”;

}

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

1 Like