Testing Objects for Properties - don't understand "checkProp"

Testing Objects for Properties - don't understand "checkProp"
0

#1

I am having a hard time understanding this challenge. I completed it with some help from the forum, but I am having a real hard time understanding where “checkProp” comes into play, as I do not see it defined in any other part? Could we have used any other word in place of “checkProp”? Could someone please clear this up


#2

can you provide the code or link to the challenge?


#3

Oh yeah, sorry about that - here is the completed (correct) code for the challenge. function checkObj(checkProp) is what I don’t get

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


#4

I’ve reformatted the code below to make it easier to read. You can include code in post by using triple back apostophe’s (`) … top left key on keyboard, under escape usually.

checkProp is the name of the argument to the checkObj function. It is used in the call to hasOwnProperty. The value of checkProp is whatever is passed to the function, which in this case is “gift”, from checkObj(“gift”) at the bottom. So the call to the hasOwnProperty will be myObj.hasOwnProperty( "gift"). Gift is a property of the myObj object, so this will return true and return the property value from the checkObj function.
To see this, try:

var val = checkObj( "gift");
console.log( val); // this will display "pony" in the console

hope this helps

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

#5

Oh ok, got it. thanks for the help, now I understand it a better. So - theroetically, we could change “checkProp” to “x” or “random”, or any thing we’d like?

Also, I always wondered about the ` to format code on here. Thanks for that as well


#6

pretty much anything yes. glad I could help