Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

function checkObj(obj, checkProp) {
  // Only change code below this line
 var checkObj = ({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") 
   return "pony",
 var checkObj = ({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")
    return "kitten",
 var checkObj = ({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")
    return "Not Found";
 var checkObj = ({city: "Seattle"}, "city")
    return "Seattle";
 var checkObj = ({city: "Seattle"}, "district")
    return "Not Found";
 var checkObj = ({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")
    return "Not Found";
}
  // Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.46

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

I am completely lost. I was able to have the first variable to return ‘pony’. And I’m sure I have this all wrong after that. Can you point me towards a few steps that would help me figure this out?

step 1 is going to have to be to reset this exercise back to its initial state as what you are doing is not correct and cannot be fixed.

I would suggest starting by looking at the example and the text of the step to understand the lesson itself first.

for eg.

const myObj = {
  top: "hat",
  bottom: "pants"
};

myObj.hasOwnProperty("top");
myObj.hasOwnProperty("middle");

in the given example, what were they trying to teach us?

Let me know what you think (please give a detailed answer) and we can go from there.

After looking closer, this is what I have.

  var checkObj = ({ 
    gift: "pony", 
    pet: "kitten",
    bed: "sleigh"
    });
  var checkObj = ({
    city: "Seattle"
  });
    
  checkObj.hasOwnProperty("gift");
  checkObj.hasOwnProperty("house")
  return "Not Found";

I still cant seem to find a way to return Gift, Pet, and City to their corresponding values.

please reset again.
Here’s the original code

function checkObj(obj, checkProp) {
  // Only change code below this line
  return "Change Me!";
  // Only change code above this line
}

You need to add code between the two comment lines without changing anything else.

You do not need to define an object

you are -given- an object in the parameter list for checkObj function

you also do not need to hardcode any property. You are given that property in the parameter list as well.

Ok, In this example. Is the obj = top:?
And is the property = “hat”?

Or should I be using something different like an if/else statement?

in the example the object is the const myObj variable

and hasOwnProperty is being used to check if a specific property exists in that object or not.

Ohhhh

if (myobj.hasOwnProp(checkProp)){
   return myobjective[checkProp];
}
else {
  return "Not Found";
}

because the parameter list is called above?

I changed some details to hide the answer.

the parameters are what the functions gets, yes.

(you call the function and pass it what it needs)

Got it now. That was a biggie. Do you possibly have a better to remember that? Or should I just do it over and over until it sticks?

to remember ? (not sure what it is you are trying to remember?)

Lol, My apologies. I understand it. Thanks.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.