Stuck On JS, Testing Objects for Properties

Below I have been working on changing the switch statement into an Object to pass this lesson, I set result to Not Found by default and created the object lookup like in the example and gave a key/pair value of obj and hasOwnProperty, how would i update result so that it updates only if hasOwnProperty is true…

  **Your code so far**

function checkObj(obj, checkProp) {
  // Only change code below this line
  let result = 'Not Found'
  const lookup = { 
      obj: obj.hasOwnProperty(checkProp)
  }
  result = lookup[obj];

  return result;


  /* switch (true){ 
    case obj.hasOwnProperty(checkProp):
    return obj[checkProp];
    default:
    return 'Not Found';
  } */

  /* if (obj.hasOwnProperty(checkProp)){
    return obj[checkProp];
  }
  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/97.0.4692.71 Safari/537.36

Challenge: Testing Objects for Properties

Link to the challenge:

Hi @sandiegoexclusive !

Welcome to the forum!

This challenge stumps most people.
Most people think they have to create their own objects.
But you don’t have to go through all of that trouble and create your own object here

Nor do you have to use a switch statement here.
You could if you really wanted to, but I think an if statement would be easier in this case.

The simplest answer is at the very end of your code. (Hint:look at the last if statement) :grinning:

Hopefully that makes sense as to why you don’t need to create your own object.

If you console.log(lookup[obj]) then you should be getting back undefined because you are missing the quotes inside the brackets

Also, remember that hasOwnProperty returns a boolean.
If you add this console.log(lookup['obj']) then it would return either true or false.
Then this line here would assign that boolean to the result variable

 result = lookup['obj'];

Right now your code is return true or false

Well, you could add a conditional for the value like this.

function checkObj(obj, checkProp) {
  // Only change code below this line
 
  const lookup = { 
      obj: obj.hasOwnProperty(checkProp) ? obj[checkProp]:'Not Found'
  }

  return lookup['obj'];

  // Only change code above this line
}
checkObj({city: "Seattle"}, "district")

However, this is a lot of extra work.
I still think the if statement without creating an object is the way to go :grinning:

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