Testing Objects for Properties -How do we do this?

Testing Objects for Properties -How do we do this?
0

#1

Tell us what’s happening:
I think I am supposed to use an if /else statement to fix this but the logic doesn’t make sense to me.

Your code so far

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

function checkObj(checkProp) {
  // Your Code Here
if myObj.checkObj(checkProp) return "";    // true
myObj.checkObj();  
  return "Change Me!";
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36.

Link to the challenge:


#2

You could use an if statement, but all you really need to do is change the return statement.

Change:

  return "Change Me!";

by replacing “Change Me” after the return to some code which will evaluate to true or false. The challenge instructions’ Example does a great job in showing how to use the hasOwnProperty function to do just that.


#3

https://www.freecodecamp.org/challenges/testing-objects-for-properties

return “Not Found” doesn’t work. Can you tell me what’s wrong with my code?


#4

You need to copy/paste your code into a post for me to see what you currently have.

When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


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

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

// Test your code by modifying these values
checkObj("house");

#6

Please read the last post of mine so you can properly format your code next time to make it more readable. Thank you.

You need to be using the hasOwnProperty in your if statement. You have:

if (checkProp) {

In the example of checkObj(“house”) , your if statement be making the following comparison:

if ("house") {
// the above if statement will evaluate to true but since "house" is not a property 
of myObj, your return myObj[checkProp] will return undefined

Also, not sure why you have the following on a line by itself above the if statement:

myObj.hasOwnProperty(checkProp);

It is not going to help you on that line by itself.


#7

Got it. Thanks for your help. I’ll make use of the back ticks next time for posting my code.


#8

Tell us what’s happening:
Please, could someone help me figure this out?!

I found this a bit challenging and but after playing around I got the code below - which gave me an error for the last test (checkObj(“house”) should return “Not Found”).

I went onto the forum for some much-appreciated guidance and found someone had created this solution which worked, but for the life of me I can’t see the error or difference in my code. I also added “return “Change Me!”;” but the last test did not work:

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

return “Change Me!”;
}

Your code so far

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

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

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge: