Just one more check✔️ away , help!

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line
obj={
  "gift":"pony",
  "pet":"kitten",
  "bed":"sleigh",
  "city":"Seattle"
};

if(obj=obj[checkProp]){
  return obj;
}
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/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62

Challenge: Testing Objects for Properties

Link to the challenge:

Only one mark away? Good, given this code is completly wrong ^^°

For a start, the function is getting passed an object - you are not supposed to create the object within the function.
Then look at the task: You are supposed to return the VALUE of the PROPERTY. Right now you are returning the object.
Plus while we are at the “read the task” part → read the task again, notice how the example uses this “.hasOwnProperty()” thing? Maybe you should have that in your code somewhere :wink:

Edit: By the way, it’s passing the tests because your if has an assignment instead of an comparison. Which is another error in the code and basically passes the tests by coincidence.

1 Like

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62

Challenge: Testing Objects for Properties

Link to the challenge:

The object you need to test is passed to the function already, you’re not supposed to create your own object. That where the issue is

1 Like

Please don’t open several topics with the same question.

Also I edited my post in your last topic because at first glance I identified the wrong error and had to change it - please go back there, read my post and try again :wink:

still same problem

function checkObj(obj, checkProp) {

  // Only change code below this line

    obj={

    "gift":"pony",

    "pet":"kitten",

    "bed":"sleigh",

    "city":"Seattle"

  };

if(obj.hasOwnProperty(checkProp)){

  return obj[checkProp];

}

else{return "Not Found"};

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

HI @gautamjiyadav077 !

As the others have mentioned, delete this

Your goal is to create a function that works for any object.
Not just one you created yourself.

What if you have to test your function with a 100 different objects?
Or 100’s of key value pairs?
Are you going to hardcode all of that?

No :wink:

When you call the function like thischeckObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") that is where you pass in the actual object.

Hope that makes sense!

2 Likes

ok
let’s try this out

still not working
please help

What’ your new code?
Please post it here so we can see the issue.

actually
i have no idea what to do
i thought about what u said
but no conclusion at all
hope u understand

What happens when you deleted the object?

Most people are confused by this lesson, because they think they have to create their own object.
But that is incorrect.
That is why we have been telling you to remove it.
Hardcoding an object is not practical because your function should work for any object.
Not just one you hardcoded yourself.

make sense?

1 Like

what do you actually mean by ‘my own object’
i made object according to what being asked on the left side
and the one in which i am getting a cross instead of a check( the last one)
is also there in the object i created.
thanks mam

The left never asked you to create an object.
Look at the function, it’s taking in an “obj” and if you look at the left, you see the thing you created is actually passed into the function directly.

You dealt with function before, haven’t you? These also got given arguments which they then use within, without having to actively write them in the code.

You created this here

But you are not supposed to.

Let’s take a close look at what this part of the code says here

if(obj.hasOwnProperty(checkProp)){

  return obj[checkProp];

}

else{return "Not Found"};

What that is saying is if the obj has a property of checkProp then return its value otherwise return Not Found.

Remember that obj and checkProp are parameters and act as placeholders for the real values for when we call the function.

This is an example function call

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

You can see in that function call we have an object

{gift: "pony", pet: "kitten", bed: "sleigh"},

and a property name

 "gift"

With that function call, we check if object has a property of gift.
Since it does, then we return the value which is "pony"

Here is another function call that I just made up

checkObj({name: "Jessica", country: "USA"}, "city")

With this function call, we are checking if the property of city exists in this object here

{name: "Jessica", country: "USA"}

Since city is not a property, then we return Not Found.

Your current code only tests for these given properties

 obj={

    "gift":"pony",

    "pet":"kitten",

    "bed":"sleigh",

    "city":"Seattle"

  };

But what about these given properties I just made up?

{name: "Jessica", country: "USA"}

Are you going to add that to your object you created?
No.
That is not practical at all.
We don’t want to continually have to add properties to an object we created just so we can test our function.

Hope that is clearer

2 Likes

why don’t u just tell me where to correct this code
plz understand

These here are just a few test cases

But you want to create a function that works for any object, not just objects freeCodeCamp created for this challenge.

Hope that helps

3 Likes

now i totally understand what you actually mean but have no idea how to what to code for that…
btw thankyou very much mam
for saving ur time for me

1 Like

Well the fix is pretty simple.

Keep the if/else statement and please reread through my last question here

1 Like