Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:
I don’t even know where to get started with this task Where on earth did pony, kitten, etc come from? Can anyone help me get started?

Your code so far

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

The test cases supply different values for the obj and checkProp… The logic inside of your function needs to work for any object and property.

I’m not sure what that means but I’ve got this far with a bit more luck but am now stuck again:

function checkObj(obj, checkProp) {
  // Only change code below this line
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};
  checkObj.hasOwnProperty("gift");
  checkObj.hasOwnProperty("pet");
  return "Not Found";
  // Only change code above this line
}

Why are you adding all of this? What is your logic to solve the challenge?

Do you understand what the parameters of the function represent? Do you understand how functions pass values (arguments) to functions and how they can be referenced inside a function?

You must not attempt to put a specific object inside of your function.

You must not attempt to use specific hard coded attribute names.


obj is a variable. checkProp is a variable. You must use these variables. You must not redefine these variables.

Absolutely no idea, to be honest. I’m guessing.

Random guessing will almost never solve the problem. You need some sort of plan or idea to work towards.

Ok. Can you help at all please?

I’m trying to help…

Do you know what a variable is?

Do you know what a function argument is?

Do you know what a function call is?

The tests list several function calls that will be executed with your function. Those function calls have specific values for the arguments obj and checkProp. You can inside of your function use obj and checkProp like any other variable.

So the instructions are asking you to write the logic to determine if the object obj has the property checkProp.

I’ve started again and am stripping it back to:

function checkObj(obj, checkProp) {
  // Only change code below this line
  checkObj.hasOwnProperty("obj");
  checkObj.hasOwnProperty("checkProp");
  return "Not Found";
  // Only change code above this line
}

Still no good. I can’t quite understand what it’s asking?!?

It really helps if you read what I write and ask questions about any parts of what I write that don’t make sense.

checkObj is not defined anywhere

No test case is asking about a property literally called "obj"

No test case is asking about a property literally called "checkProp"

Putting the quotes in there means that you want a property with that literal name.

Tell us what’s happening:
Hi - I’m still on this and have tried a variety of approaches with no success based on what I think it is saying. Can anyone help please?

Your code so far

function checkObj(obj, checkProp) {
  // Only change code below this line
  checkObj.hasOwnProperty("checkProp");
  return "Not Found";
  // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

Modify the function checkObj to test if an object passed to the function (obj ) contains a specific property (checkProp ). If the property is found, return that property’s value. If not, return "Not Found" .

The checkObj does not exist. You cannot use a variable that is not defined… Only obj exists

You are not looking for a property called "checkProp", you are looking for a property with the name stored in the checkProp variable

Also, your return statement is going to always say that you did not find the property. You will need some logic to return differently based upon if obj has the property given by the name stored in checkProp

So, is it something like this?
obj.hasOwnProperty(“”) ?

What do you mean by looking for the property in checkProp but not looking for checkProp?

Closer.

If you use quotation marks, you want a property that exactly matches the contents of the quotes.

So

obj.hasOwnProperty("foo")

Is looking for

obj.foo

Ok, cool. So what property is the question asking me to find? I keep reading it and it sounds like:
obj.hasOwnProperty(“checkProp”);
return “Not Found”;

Try running a test case and logging out what is in the variable

function checkObj(obj, checkProp) {
  console.log(obj);
  console.log(checkProp);
}

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

Please could you remind me how to do this?

Literally copy the sample code I gave you?

I tried to give you everything you need to make a sample function call that logs out the values of the arguments.