Where are the values for checkObj?

I’ve completed the challenge but I don’t understand where the console values (gift: “pony”, pet: “kitten”) are stored? I always write the code in my code editor when I’m finished, but I don’t know how to do that with this?

This is my code but its very basic and is not a function. How would I make this into a function?

var myDog = {

  name: "Martha",

  legs: 4,

  tails: 1,

  friends: ["Benson", "Willow", "Tabby"],

};

myDog.hasOwnProperty("name"); // true

myDog.hasOwnProperty("bark"); // false

Any help would be great

Thanks, LC


function checkObj(obj, checkProp) {

if(obj.hasOwnProperty(checkProp)) {
  return obj[checkProp];
} else {
  return "Not Found";
}
}

Challenge: Testing Objects for Properties

Link to the challenge:

Hello there,

They are not necessarily stored anywhere. You can see what is passed to your function in the tests:

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

You are tasked to write a function that can check any object for any property. You can test it, by passing any object, and any property.

Hope this clarifies.

1 Like

So in the function checkObj(obj, checkProp) the checkObj is just the function name, and obj, checkProp is the object and the property?

So if I had this code …

var myDog = {

  name: "Martha",

  legs: 4,

  tails: 1,

  friends: ["Benson", "Willow", "Tabby"],
};

Would this be correct?

function checkObj(myDog, name){

  if(myDog.hasOwnProperty(name)) {
    return myDog[Name];
  } else {
    return "Not Found";
  }

}

Correct.

Not quite. You need to remember, JavaScript is case sensitive. Also, the names you have chose for your function parameters are not ideal, because objects other than myDog could be passed to the function, and properties other than name could also be passed.

To use the checkObj function on the myDog object you have created, it would look like:

checkObj(myDog, "name")
2 Likes

Thank you I understand now :slight_smile:
One last thing I’m getting a syntax error with "name"

var myDog = {

  "name": "Martha",

  "legs": 4,

  "tails": 1,

  "friends": ["Benson", "Willow", "Tabby"],

};

function checkObj(myDog, "name") {

  if (myDog.hasOwnProperty("name")) {

    return myDog["Name"];

  } else {

    return "Not Found";

  }

}

Right. Now this is the difference between a string and a variable:

var myString = "I am a string";

myString is the variable, but "I am a string" is a string. myString has a value of "I am a string".

  • Values are passed to functions as arguments:
myFunc(myString); // This works
myFunc("I am a string"); // This also works
  • Parameters are variables, and come from function arguments:
function myFunc(someVariable) {...} // This works: You can use someVariable within myFunc

function myFunc("I am a string") {...} // This does not work: cannot be used within myFunc

As you go further through the JS course, this will make more and more sense.

Thank you very much for your help, I understand the core bits the this but not the way the function runs. I will have to come back to it later on

Thanks Again !