Basic JavaScript: Testing Objects for Properties (Help question)

So if obj.hasOwnProperty(“checkProp”) is true then return the value of checkProp. Or else, return “Not Found”. The tests are passing for “Not Found” but why isn’t it returning the value for checkProp?

I know a list of objects and their properties exist in the lesson because of what the test conditions say. But the lesson never directly mentions creating an object and its properties to test for. I stopped the help video before he started typing code to avoid getting any ideas that would pass me, same goes for the hint/help forums. I’d rather get the theory and apply the code myself, but the video is only a minute long so there is no way he actually wrote an entire object list with its properties in the video lol.

Your code so far


function checkObj(obj, checkProp) {
// Only change code below this line

if (obj.hasOwnProperty("checkProp") === true) {
 return checkProp;
} else {
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/83.0.4103.97 Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

1 Like

your code is failing to the below point

and also here

Now get to the point, you are using obj it comes from the function argument, this is fine that you kept it same, but you changed the next argument checkProp, you should keep it same, not in the quote mark.

then look at this code for your understanding how returns the result:

var myObj = {
  top: "hat",
  bottom: "pants"
};
// top and bottom are two properties of myObj
// if you want the value of any property you need this code

return myObj[thePropertyYouNeedTheValue]
// in your code myObj is obj, and property is checkProp
// now you decide how you make the return statement if it has its own property
1 Like

I appreciate the breakdown of the challenge that you have outlined for me! I passed the challenge with this code:

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

I still think the challenge itself should have specified the object and properties you needed to pass the challenge, like city:“Seattle”. Instead I was wondering why I see the objects and properties in the tester but not in the lesson itself. Thanks so much for the help!

1 Like

Upon further review I was complaining about it for no reason lol. The code passed without the list I believe it was just the checkProp/checkProp[obj] mistake I was making and the “” marks I used around “checkProp”.

1 Like

Since, the function is not called in the lesson, you do not need to set the value of obj, rather behind the scene, FCC arranged everything for testing your code.

And if you want to test it yourself follow the below steps :
go to here live programming
Select JavaScript ES6 from the top-left, copy and paste your code without setting the value to obj inside the function there,
now declare a var object outside the function
then call the function passing the var name instead of obj and a property name instead of checkProp.
Then see the step by step output of your code live there.

Hope you will get a clear concept by this way.

Thank you.

1 Like