freeCodeCamp Challenge Guide: Testing Objects for Properties

if (myObj.hasOwnProperty(checkProp)) { 
    return myObj[checkProp];
}
else return "Not Found";

3 Likes

Hi codecampers!

I didn’t have much trouble with this, but why this isn’t working?

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

function checkObj(checkProp) {
if(myObj.hasOwnProperty(checkProp)){
return myObj.checkProp;
}else{
return “Not Found”;
}

return “Change Me!”;
}

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

2 Likes

I used this simple solution

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

function checkObj(checkProp) {
// Your Code Here
if(myObj.hasOwnProperty(checkProp)){
return myObj[checkProp];
}
return “Not Found”;
}

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

1 Like

I’m a little bit frustrated if I’m honest.

I’m understanding a lot of the logic and I knew this needed boolean to solve and in fact my answer contained pretty much all of what was required - so I’m doing something correctly, however, I’m causing myself many problems with arguments/parameters and for some reason I can’t quite fathom which argument to pass through whatever function.

When I mentioned I had the answer, I did but I also created a for loop, thinking I needed to iterate through myObj and then, I was passing the for loop (i) through myObj and when that didn’t work I tried passing checkProp through, which is obviously correct but the rest of my code was wrong because of the loop so if failed but it’s the arguments that are really killing me.

I’ve learned a lot and I feel I’ve made good progress - take away the argument issue and it’s all making sense, well mostly but passing arguments through things is making me feel lost again.

Has anyone else struggled with arguments and what did you do to overcome them and fully understand them?

1 Like

What I don’t understand is how checkProp comes into play. It doesn’t look like it’s defined. I see that checkProp corresponds to the object property.
Does if(myObj.hasOwnProperty(checkProp)) basically check if an object has a property, then returns True or False?

8 Likes
// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here
  if(myObj.hasOwnProperty(checkProp)==false)
    {
      return "Not Found";    
    }
  return myObj[checkProp];
}

// Test your code by modifying these values
checkObj("gift");
1 Like

Yes. The .hasOwnProperty will always return a boolean (true or false). Think of a really simple if then else statement.

   IF (some check)
        THEN do whatever action for positive result;
   ELSE (do another check)

We know that if the result of that 'IF" statement is TRUE then it proceeds to run the “THEN” code. However, if the result of the statement is FALSE, it’ll skip to the “ELSE” and evaluate that statement.

Coming back to our object - if our object has the property we’re checking for, it’ll evaluate as TRUE and move on to the next line of code which (in the example from the post above) returns the value of that object’s property.

Hopefully that explanation did more help than harm!

1 Like

To clarify, what isn’t making sense to me: Is checkProp a term used specifically to check if a property exists or could any name be used in the place of checkProp…

7 Likes

How come dot notation does not work here?

return myObj[checkProp];

return myObj.checkProp;

6 Likes

can anyone explain why myObj.checkProp doesn’t work in this case?

4 Likes

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

function checkObj(checkProp) {
// Your Code Here
var prop = myObj[checkProp];
var val = myObj.hasOwnProperty(checkProp);
return val ? prop : “Not Found”;
}

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

2 Likes

Did you get the answer to why . isn’t working.

I believe that you need to add the parentheses after checkProp to use it that way

Hello everyone, this was my solution:

var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  if (myObj.hasOwnProperty(checkProp)){
    return myObj[checkProp];
  }
  return "Not Found";
}

checkObj("gift");

Came here as I couldn’t work out why using dot notation for my first return statement returned undefined. Still a little unclear :confused:

Original:
return myObj.checkProp;

1 Like

No, I still don’t really get why we couldn’t use dot notation.

Hi Tanikar, check this link Click here
[ ] should be used when property names are to be dynamically determined.

If you know the name of your val, then it’s better to use dot(.) for example:

var animal={
cat: “Molly”,
dog: “Shep”
};
// animal.dog;

And if you are using Prop(when there are many values):

var animals = {
cat: “Molly”,
dog: “Chep”,
mouse: “Shiny”,

};
function checkAnimals(checkProp) {
if(animals.hasOwnProperty(checkProp)){
return animals[checkProp];
}else{
return “I don’t have this type of animal”;
}
}

Please :heart: if it helped. Good luck!

21 Likes

thank you very much @llimaVIP!

2 Likes

Sorry for the late response. Check this out https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable

4 Likes

yes, thank you very much!!!

no worries. Thanks for sharing the link!