How to pass the Testing Objects for Properties challenge

Tell us what’s happening:
Any help on how to pass the ‘Testing Objects for Properties’ challenge. The code I’ve written doesn’t work although various forum posts as well as some youtube channels have given it as the solution. The problem is that the challenge checks my code against an object even though the challenge doesn’t define any objects not does it require for me to create any objects. For example, here’s the first of about seven tests that my code is checked against:

checkObj({gift: “pony”, pet: “kitten”, bed: “sleigh”}, “gift”) should return the string pony.

Everything was working fine for me up till this point. And now I can’t go beyond this point of the course. Need I say this is really frustrating.

  **Your code so far**

function checkObj(obj, checkProp) {
// Only change code below this line
function checkObj(obj, checkProp) {
if (obj.hasOwnProperty(checkProp)) {
  return obj[checkProp];
} else {
  return "Not Found";
}
}
// Only change code above this line
}

checkObjcheckObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")
  **Your browser information:**

User Agent is: Mozilla/5.0 (Linux; Android 9; TECNO CC9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.58 Mobile Safari/537.36

Challenge: Testing Objects for Properties

Link to the challenge:

Hi faisal

I think the problem is in “return obj[checkProp]”. You should convert this value to a string . You can use the method “toString()”. Here is my answer:

function checkObj(obj, checkProp) {
  // Only change code below this line

  const hasPropertie =  obj.hasOwnProperty(checkProp); 
  if(hasPropertie){
    return obj[checkProp].toString();
  }else {
    return "Not Found"
  }
  
  // Only change code above this line
}

You were right in the logic. It was just a small detail.

Your solution is not correct.


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.

You have defined the function inside of itself. That can’t be correct.

Yh that makes sense. Thanks.

No it isn’t. Thanks for pointing it out.

I improve your decision

 return obj[checkProp] || "Not Found"; 

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.

1 Like

This post was flagged by the community and is temporarily hidden.