Testing Objects for Properties in javascript

there is an issue with your code. The easiest way to explain the issue would be if you explain why you have written the code in the way you did, particularly this part below

  var obj = {

gift: "pony",

pet: "kitten",

bed: "sleigh",

city: "Seattle"


1 Like

i am beginner sir. i am having this issue the help me out about this

if there is something wrong then would you make me understand i am beginner in coding

its in the code challenge. then what else I would say

but it is not in the challenge
this is the starting code, the object is not there

can you say why you add that?

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


so i have made a mistake then help me directly

the best way to explain depends on your answer to the question

We are trying to establish what you do and do not understand. Without that, my explanation would have to start at the basics of how a function works and would be very long.

i got it. i shouldnot added in the code.

Sure, but do you know why that shouldn’t be there? Understanding how function arguments work is important.

i got it sir. thanks for responding. i am beginner so didnt get you earlier.

yeah i want to know this.

Your checkObj function is passed an object as an argument, it gets passed to the obj parameter.

You are overwriting the parameter obj with the object you created inside the function. The parameter and the object share the same identifier name.

function checkObj(obj, checkProp) {
  // Only change code below this line
  var obj = {
    gift: 'This value is not in the object passed to the function',
    pet: 'kitten',
    bed: 'sleigh',
    city: 'Seattle',
  if (obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
  } else {
    return 'Not Found';

console.log(checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'gift'));
// This value is not in the object passed to the function
1 Like

Hi, I had a similar question and Randall explained it very succinctly in the bottom of the thread:

What you are wondering is what I was wondering. You do not have to spell out those variables as you did in var obj.

If you set up the function at the top, checkObj correctly with the if statement, it will return obj[checkProp].

when you console.log you want to set it up as lasjorg did with the gift: ‘pony’, etc, in the parentheses to log the result correctly.

If you tried to console.log all those parameters with different values like

gift: ‘cat’ , pet: ‘dinosaur’ , bed: ‘waterbed’

That’s when you will get the Not Found

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