Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:
i am at a loss.

Your code so far

function checkObj(obj, checkProp) {
  // Only change code below this line
var obj = {
  gift: "pony", 
  pet: "kitten", 
  bed: "sleigh",
  city: "Seattle",
  };
if (obj.hasOwnProperty(checkProp)){
  return obj[checkProp];
}
else {
  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/107.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

What is this doing for you?

the instructions are
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" .

  • Failed: checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") should return the string pony.

  • Failed:checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet") should return the string kitten.

  • Passed:checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house") should return the string Not Found.

  • Failed:checkObj({city: "Seattle"}, "city") should return the string Seattle.

  • Passed:checkObj({city: "Seattle"}, "district") should return the string Not Found.

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

I can read the instructions. The instructions don’t tell me why you decided to write that part of the code though

Hi @axo5232 !

Most people get tripped on this problem.
As mentioned earlier, you are not supposed to hardcode your own object here

The goal of this lesson is to teach you how to work with functions and have any object that is passed into the function. Not one that you hardcoded that just handles the test cases.

Right now, your function only works for this object you created here.

But what if I wanted to add more test cases and test this function with 100 different objects.
We don’t want to be in a situation where we have to hardcode 100 different objects or keep adding more properties to an existing object just to pass the tests.

I should be able to pass in any object I want to with your function and have the function return the correct output.

For example, try testing out this function call with your current code.

 console.log(checkObj({name: "freeCodeCamp", type: "education platform", isAwesome: true}, "type"))

You should see an output of “Not Found” .
But that is not correct because there is key called “type” here
type: "education platform" when we call the function.

Hopefully that clears up the confusion on why you shouldn’t hardcode your own object in this challenge.

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