Testing object for properties?

Tell us what’s happening:
Got all checks passed except the one

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

I guess I have to check it once and then remove the ‘pony’ from the object. so it will get a not found

tried to do delete myObj[“pony”];
but that diden’t work.

Your code so far


function checkObj(obj, checkProp) {
 // Only change code below this line
 
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh",
city: "Seattle",
};
if (myObj.hasOwnProperty(checkProp)){
 return obj[checkProp];
}
else {
 return "Not Found";
}
   
 

 

 // Only change code above this line
}

Challenge: Testing Objects for Properties

Link to the challenge:

Hi and welcome to the forum!

Why are you defining an object inside of the function? This function should check if the argument obj contains the target property.

1 Like

well I did define the object there because of these… I assumed that I had to do everything inside of these. Or I am allowed to define stuff outside of these?

// Only change code below this line
// Only change code above this line

Yeah, you need to keep your code between those two comments.

But why did you define the object at all? You need to check the passed in obj and not this hardcoded myObj.

1 Like

I thought I could define the object and refer to it when I did
if (myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];

Sure, you can, but then your function is not checking if the obj used in the function call has the checkProp. The function call only checks the hardcoded myObj.

The challenge wants the function to work for any obj.

2 Likes

ah I see thanks. I’m just a little bit clueless to how to pass them to obj. I guess I would somehow have to do something like this.

return (“My object”, checkProp); ?
Im just a little bit confused to how to assign my object gift, pet, bed to obj without using return.

In this line right here, the obj to check and the checkProp to find are already defined for you.

So in the test you referenced,

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

the function call defines the variables

let obj = {pet: "kitten", bed: "sleigh"}
let checkProp = "gift"

No one is telling you not to use a return. You shouldn’t be writing your own object.

Take a look at your logic here:

You are checking whether an object that you created contains a property, then returning that property in a different object that you received as an argument.

You never need myObj. All of the checks should be on obj.

1 Like

so instead of like creating myObj I could just call it obj like arg1 is refering to and then do like.

if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];

}

obj = {
gift: "pony",
pet: "kitten",
bed: "sleigh",
};

stop assigning objects to stuff. you already have the object to check, you don’t need to create it

2 Likes

So the object already exists from the beginning? even if you can’t see it among the code?
Ah tried this and now I passed. Thanks everyone for your help :slight_smile: .
I assumed I had to create the object because I diden’t see it.

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

it’s passed in when the function is called

1 Like

Yep. The act of calling the function with the correct arguments means that obj is an object and checkProp is a string already.

1 Like