Return property if object is found

Hi

Can anyone tell me what’s wrong with my code please?

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line
result =""
var obj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh",
  city: "Seattle",
 
}
if(obj.hasOwnProperty("checkProp")){
  result = "checkProp"
}
else {
  result = "Not Found"
}
return result

// Only change code above this line
}
checkObj("pony", )
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68.

Challenge: Testing Objects for Properties

Link to the challenge:

I suspect that you are not supposed to return the string “checkProp” when you find the property.

1 Like

Hi

I cant see where I am going wrong. I played around with it a bit but I did get far in one go. And although its not correct, I had a an inkling how to approach it.

But I dont get obj as a paramatar and variable or object.

   **Your code so far**

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

Challenge: Testing Objects for Properties

Link to the challenge:

Why are you redefining the obj?


Also, be careful - variable are CaSE SeNSiTivE.

2 Likes

Also checkProp shouldn’t be a string.

1 Like

Hi Jeremy

So I got rid of the the var result = “”, however I thought it would have worked and I could have stored the data the depending on the result of the condition. But I could only get 3 right this way and they were all if the condition was false.

Do you have any idea why I could not have use var result = “”. Also

now I have changed and used two return statements and I 'm getting all but one correct. Any idea whats going on?

Please post copy-pasted code instead of a screenshot.


Also,

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"

  }

can you answer the question of why you are redefining obj?

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 (’).

Which part of the code?

Right here. You are redefining the input variable obj.

I’ve only defined it once?

Oh okay. So is object defined else where?

It is a function input. You are overwriting the input, which is why your function fails with some inputs.

the checkObj it´s out of the code you can change :slight_smile:

checkObj is the function being tested

Here is my answer -

function checkObj(checkProp) {
        
        result =""

        var obj = {
            gift: "pony",
            pet: "kitten",
            bed: "sleigh",
            city: "Seattle"        
        }

        if(obj.hasOwnProperty(checkProp)){
            result = "Yes !"+checkProp+" property exists"
        }
        else {
            result = "Not Found"
        }

        return result

        // Only change code above this line
    }

    let checkProperty=checkObj("gift" );
    console.log(checkProperty);

Hi @rameshkmunjal !

Welcome to the forum!

I originally blurred this out without checking if it was the correct answer.
But if you run it there are errors in the console.
Plus, you changed the function parameters.
function checkObj(obj, checkProp)

We don’t advise forum users to post their answers.
It is best to guide the OP to the correct answer.

Thanks!

I understand your confusion when you are starting out. Think of obj as some data that you want to access from a database on a server. The data is already there and your job is to use a function to pull it and display it in the console.
Think of it like if the object had over 100 items or more will it be reasonable for you to redefine it?