Question about how to solve Testing Objects for Properties in JavaScript

Tell us what’s happening:
I have a question about how to solve Testing Objects for Properties in JavaScript Algorithms and Data Structures.
Describe your issue in detail here.

I have tried various versions of the code, posted in the forum, that has worked for other people but without success. I have included two versions of the solutions in my below code.

  **Your code so far**
var myObj = {

    gift: "pony",

    pet: "kitten",

    bed: "sleigh",

    city: "Seattle"

  };

  function checkObj(checkProp) {

// The below code does not return the true tests

myObj.hasOwnProperty(checkProp)

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

  }

// The below commented out code does not return the true tests

/*   if(myObj.hasOwnProperty(checkProp)){

return myObj[checkProp];

      } else{

        return 'Not Found';

  }

// };

}    // This closes the function

  */

// Test code by modifying these values

checkObj('gift');

What am I doing wrong? Thank you

  **Your browser information:**

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

Challenge: Testing Objects for Properties

Link to the challenge:

Why are you defining myObj before the function and then referring to it inside of the function? You do not need myObj at all so get rid of it.

You also changed the function definition. It is supposed to be:

function checkObj(obj, checkProp) {

The object you are testing is being passed into the function with the variable name obj.

My suggestion, click the Reset All Code button to start this challenge over fresh. Do not change anything that is already there, just add code between the comments.

" Why are you defining myObj before the function and then referring to it inside of the function? You do not need myObj at all so get rid of it."
That is how it is done in all the solutions I have seen in the Get Hint thread for the problem. freeCodeCamp Challenge Guide: Testing Objects for Properties - Guide - The freeCodeCamp Forum
For instance, this was posted by someone in the thread:
This is how solved it.
must be a better way but it works.

// Setup
var myObj = {
gift: “pony”,
pet: “kitten”,
bed: “sleigh”
};

function checkObj(checkProp) {
// Your Code Here
if(myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];
}
else if(myObj.hasOwnProperty(checkProp) !== true){
return “Not Found”;
}
else{
return “Change Me!”;
}
}

// Test your code by modifying these values
checkObj(“gift”);

You are copying old answers for a version of the challenge from many years ago. Copying old answers can lead you astray! Those answers don’t work at all anymore.

You need to reset the code for this challenge. The function you have doesn’t have the same number of arguments that the challenge requires.

Yes, of course. But I haven’t seen any code in the Get Hint link that works. If the 2 years worth of advice from people is obsolete, why isn’t it deleted and replaced by useful, more recent, suggestions? I could learn from Get Hint, seeing alternatives from other learners, but it is rather frustrating when it wastes my time and leaves me with nothing.

The actual solution listed in the guide is up to date. If you choose to copy other people’s old answers and disregard the “only change” comments in the starter code, we can’t really protect you from your decisions:

function checkObj(obj, checkProp) {
  // Only change code below this line
  return "Change Me!";
  // Only change code above this line
}

I’m not big on the idea of clobbering all previous discussion about old versions of this challenge.


Anywho, the key thing we changed in this challenge is the fact that both the obj and the checkProp are arguments to the function. Previously only checkProp was an argument but the obj was hard-coded.

This change is small, but it makes the function much more flexible because we can use the same checkObj() function for many different objects.

You use the same logic as the old solution you have above, but you need to use the function argument obj instead of the hard-coded myObj.

Besides that, just a “hint” would be very useful, instead of an entire solution to the challenge.
For instance, I have a problem understanding what the suggestion is in this code:
" checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") should return the string pony ."

“gift” on its own after the comma does not make sense to me, and checkObj({gift: “pony”, pet: “kitten”, bed: “sleigh”} is just populating the dictionary.

I’m not sure what you mean by `populating the dictionary’. That isn’t really technical terminology that fits here.


Lets look at the instructions

Modify the function checkObj to test if an object passed to the function (obj) contains a specific property (checkProp).

Ok, lets look at the given function signature now

function checkObj(obj, checkProp) {
  // Only change code below this line
  return "Change Me!";
  // Only change code above this line
}

Now lets look at a test case

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

So, this function call sets

obj = {gift: "pony", pet: "kitten", bed: "sleigh"};
checkProp = "gift";

and we need to check if the ‘object passed to the function ({gift: "pony", pet: "kitten", bed: "sleigh"}) contains a specific property ("gift")’.


You seem to have missed the Hints section right above the old answers you found:

Hints

You do not need to declare any additional variables or define any objects inside of your function. You must use the function arguments obj and checkProp.

Note: Old solutions found in replies to this post will not work. This challenge has been edited several times since 2017. Only the solution in this post is up to date.

Thank you for your prompt responses.

Are you saying this code, which is on the left side of this challenge, does not populate a dictionary? I know it is a dictionary and it seems to populate it there.

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

That is the same as this: checkObj({gift: “pony”, pet: “kitten”, bed: “sleigh”}

It seems like you are saying the variable is declared and populated in the call to the function. Is that correct? That does not make sense to me. I have not seen that in any programming study I have done and also not in a previous challenge in this course.

Yes, thank you

“Dictionary” is not a Javascript term, its a Python term. Also, “populate” isn’t really a correct term here.

This here is just a variable declaration for an object.

I am still trying to edit my previous response’s formatting so it is readable.

Okay, I am finished editing that comment. My question about the function call still stands.

Yes. That’s how function calls work. In this code

function testThis(firstArg, secondArg) {
  console.log("The firstArg is", firstArg);
  console.log("The secondArg is", secondArg);
}

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

we can see that inside of the function call

firstArg = {gift: "pony", pet: "kitten", bed: "sleigh"};
secondArg = "gift";

With any function call, you are setting the values of the arguments by invoking the function with those specific values.

1 Like

Again, I appreciate the assistance!
Maybe I would understand this better if function calls with curly brackets inside the parens were introduced before this challenge. testThis(a,b); makes sense to me as a function call, but not this. gift is right there in the brackets so testing for it in the same function call just does not make sense to me. I don’t see the usefulness, but I will play with it and move forward in the course and see if a light comes on. Thank you

So that bizarre looking f’n call is really what put me in a confused state with this challenge. The other challenges have made perfect sense after reading the suggestions on the left side.

the difference is just that a and b were given a value before
and in this case the values are passed to the function as an object literal and a string literal

let a = 7;
let b = 9;
testThis(a, b);

is exactly the same as

testThis(7, 9);
2 Likes
function checkObj(obj, checkProp) {

  // Only change code below this line

  if(obj.hasOwnProperty(checkProp) == true)

    return obj[checkProp];

  else

    return "Not Found";

  // Only change code above this line

}

const objTest = {

  length : 23,

  width : 34

}

let prop = "width";

let c = checkObj(objTest,prop);

console.log(c);

@kashyapnaman57 do you have a question? If so please open your own thread.

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 can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

2 Likes

no thank u. I thought we post answers here…

No, in general, we do not allow that as it doesn’t help anyone.

2 Likes

Here is an idea using last code:

Mod Edit: SOLUTION REDACTED