Testing Objects for Properties

Testing Objects for Properties
0

#1

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”);
`


#2

return myObj.checkProp;

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


#3

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


#4

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?


#5

Another solution can be

function checkObj(checkProp) {
// Your Code Here

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

to make the code looks cleaner


#6

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”;


#7

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


#8

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”


#11

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");

#12

Old post but, nice work!!


#13

super smart way to solve this one. :clap:


#14

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");


#15

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


#16

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


#17

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


#18

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”;
}
}


#19

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


#21

// 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”);


#22

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.


#23

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?