Testing Objects for Properties -- Almost there(?)

Testing Objects for Properties -- Almost there(?)
0

#1

Tell us what’s happening:
I am passing the first two requirements, but I cannot seem to get the code to return the “Not Found” result no matter what. I can get it to pass the “Not Found” requirement if I “flip” the code, but then the first two requirements fail. I am frustratingly close, and I’ve spent over 3 hours tinkering with this lesson. Please help.

Your code so far


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

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

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/testing-objects-for-properties


#2

As soon as a return statement is encountered, the function will immediately exit, so the second return statement will never execute. Also, when you want to do one thing when a certain condition is true versus another thing when that condition is false, you need at a minimum an if statement. I see know if statements in your code. You might want to review the following lesson.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-conditional-logic-with-if-statements/


#3

I am aware of the lesson you are referring to. It states that if a condition is false, then it will proceed to a second return statement, but even when I try to modify the code in that way, I still run into the same problems.
I will re-write it with “if” statements and show you that it still wont pass.


#4

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

function checkObj(checkProp) {
// Your Code Here
var checkProp

if (myObj.hasOwnProperty(“checkProp”) == true); {
return myObj[checkProp]; }
return “Not Found”;
}

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


#5

You can not put that semi-colon at the end of your closing parentheses. Semi-colons tell JavaScript that you are ending the statement, so the next line will no longer be treated as part of the if statement block and instead just execute.


#6

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

function checkObj(checkProp) {
// Your Code Here
var checkProp

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

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

ok, I removed the semi-colon and tried an if-else statement
Now the code skips my entire first return statement and only executes the second return statement.
As I stated before, in the lesson you referred me to it states that if the condition is false, then the first return statement will be ignored, and the second return statement (outside the curly brackets) will be executed. (and that doesn’t even require an “else” statement) No matter what I do, I can only get one or the other return statement to pass the test: Never both, which is required to pass the lesson.
I just cannot understand what I am missing here and I’ve wasted at least 10 hours re-writing this thing countless ways. Very frustrated.


#7

By the way, when I switch the order of the statements, it just simply switches which return statements pass the test, So for example, if it is testing the properties in a specific order, one or the other configurations should satisfy all requirements… But this is not the case.


#8

UPDATE;
Used a switch statement and figured it out in about 30 seconds.
Although I don’t feel it’s a comprehensive solution, it does meet all the requirements to pass the lesson.
Thank you for your help!


#9

Firstly, you don’t need this line of code actually, since checkProp is passed in as an argument or parameter (I’m not sure with the difference between these two terms) it is already declared.

Secondly, you don’t need the quotation marks around "checkProp", just remove it. If you have the quotation mark, the function will look for a property called checkProp, which does not exist, so it will consider that as false and go to the second return statement. Instead, you want the function to check for the passed in property which checkProp should represent.
I hope this solved your problem.
Good Luck!


#10

Thanks for your input Andrew.
As I mentioned, I used a switch statement strategy and successfully passed the lesson, though I kind of feel like I almost cheated because I only included the specific items in the switch that the lesson was testing for. I would have preferred to solve it in a more “conceptual” or fundamental way, but after hours and hours of frustration it felt good to finally check another lesson off the list.


#11

It’s okay to find another way to solve the challenge, and actually in most case if-else statement and switch are actually interchangeable to each other. You can try to solve it again when you have time. It’s more important to learn some stuff than simply check off challenges.
Good Luck on your coding!


#12

The particular thing I like (and just discovered) about using a switch is that you can use multiple return statements without effectively terminating the function. Good to know.
I’ve been coding as a hobby for about 15 years now, so my conceptual understanding is pretty solid. It seems in most cases the fine details like syntax, punctuation, spacing, etc. are what I get hung up on when learning new coding languages.
At any rate, onward and upward…
Cheers!