Can I get some help? Basic Javascript: Testing Objects for Properties

Asked

Viewed 43 times

-3

inserir a descrição da imagem aqui

It almost worked but I’d like to know what’s missing in the code, something else to add?

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



    function checkObj(obj, checkProp) {
      // Only change code below this line
    var myObj = {
    gift: "pony",
    pet: "kitten",
    bed: "sleigh",
    city: "Seattle"
    };
    if (checkObj = myObj.hasOwnProperty) {
      return myObj[checkProp]
    } else{
      return "Not Found";
    }
      // Only change code above this line
    }
  • The code you have presented works perfectly. Maybe there is more code that you have not shown and is generating this error.

  • @Jeanextreme002 what other code would you use? Because I’m testing numerous options here but none of them work

  • How so what other codes would I use? What I meant to you is that only this snippet that code you put in the question does not generate error.

  • with this code almost worked: Function checkObj(obj, checkProp) { // Only change code Below this line var myObj = { Gift: "Pony", pet: "Kitten", bed: "Sleigh", city: "Seattle" }; (checkObj = myObj.hasOwnProperty) { Return myObj[checkProp] } Else{ Return "Not Found"; } // Only change code above this line }

1 answer

1


What the exercise of this website asks is only to create a function called checkObj that receives two parameters (objeto and propriedade) and returns the value of the property if it exists. If the property does not exist, the function should return "Not Found".

Your problem was that you created only one parameter for checkObj() and executed its function when it was not to execute. By clicking the button "Run the Tests", the site itself performs its function by passing some test objects to check if the function works.

The solution then is just to create the function of the form below and nothing else:

function checkObj(obj, prop) {

    if (obj.hasOwnProperty(prop)) {
        return obj[prop];

    } else {
        return "Not Found";
    }
}
  • Thank you very much helped here, vlw man.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.