Basic JavaScript - Testing Objects for Properties

Hi,
Could u plz let e know what is wrong with my code:
here is the question:
Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.

Example

const myObj = {
  top: "hat",
  bottom: "pants"
};

myObj.hasOwnProperty("top");
myObj.hasOwnProperty("middle");

The first hasOwnProperty returns true, while the second returns false.


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".

and my code:

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

  **Your code so far**
function checkObj(obj, checkProp) {
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
// Only change code below this line
if(myObj.hasOwnProperty("checkProp")) {
  return myObj[checkProp];
}
else{return "Not Found";}
return "Change Me!";
// 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/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

I check it out your code and I realised that you forgot to return checkObj({city: “Seattle”}, “city”). I think there might be an error in your

1 Like

¡Hola!, tambien estoy haciendo el curso, aún no conozco todos los conceptos pero tratare de ayudarte.

Primero te comento que para este ejercicio no se requiere declarar un objeto, pero si lo ocupas para comprobar la función, debes declararlo fuera de la función.

Ya que el objeto este fuera de la función notaras que la declaración if no debe hacer referencia al nombre del objeto, sino al nombre del parametro que recibe, en este caso “obj”.

Y por último en la declaración if el parametro checkProp no debe escribirse entre comillas (No te puedo explicar la razon, ya que no usaria el termino correcto).

Si decides conservar el objeto al final pueder revisar tu función con: consele.log(checkObj(myObj, “pet”)); y deberias recibir la cadena kitten

Espero que con esto ya puedas pasar el desafío, saludos

Thank you. But could you plz explain why I even have to put city?bcuz it is not already one of the properties.
thanks

1 Like

@Honey1835 you just need make a few changes in your code in order to pass the test.

1 Like

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

2 Likes

Okay. Thank you. I just tried to help.

1 Like

And you helped a lot. :joy:
thanks.

Hi again,
I am still struggling with the code. Everything seems right but it still throws an error.please help:
function checkObj(obj, checkProp) {
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”,

};
// Only change code below this line
if(myObj.hasOwnProperty(“checkProp”)) {
return myObj[checkProp];
}
else if (checkProp==“city”){
return “Seattle”;
}

else{return “Not Found”;}
return “Change Me!”;
// Only change code above this line
}

You should not declare this extra object. You must use the object passed into the function.

I have not declared the myObj again. this is the code written by default and asks me not to change it. Here is the code I wrote:
if(myObj.hasOwnProperty(“checkProp”)) {
return myObj[checkProp];
}
else if (checkProp==“city”){
return “Seattle”;
}

else{return “Not Found”;}
return “Change Me!”;

please reset your code, notice how the function signature has

so please stop referencing a myObj that you should not have in the function

1 Like

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