Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements Use Multiple Conditions

When i preview this code works but when i check the answer it asys it's not correct. Anybody know what the problem is?

script.js
const money = 9;
const today = 'Friday'

if ( money > 10 || today === 'Friday' ) {
  alert("Time to go to the theater.");    
} else if ( money >= 50 || today === 'Friday' ) {
  alert("Time for a movie and dinner.");    
} else if ( today !== 'Friday' ) {
  alert("It's Friday, but I don't have enough money to go out.");   
} else {
  alert("This isn't Friday. I need to stay home.");
}
index.html
<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Basics</title>
  </head>
  <body>
    <script src="script.js"></script>
  </body>
</html>

1 Answer

Hi Paul!

You really need to consider the logic.

Both logical ors ( || ) should be logical ands ( && )... (you want both conditions to be true, not either or)

And for the last "else if", the test condition should be the opposite ( === ) not ( !== ).

The way it is now the logic is "if today is NOT Friday..." THEN "It's Friday, and..." (Erm...wrong!?! LOL)

Also, if it was up to me, it would be better logically to test the 50 condition first (if you have $60, you'll still just go to a movie, when you should be having dinner AND a movie!?!) because anything over $10 (including $60) will satisfy the first condition - which is NOT the desired outcome.

Either that or make the first test this:

if ( money > 10 && money <= 50 && today === 'Friday' ) { // Better first test...
  alert("Time to go to the theater.");
}

Does that make sense?

Also, whenever possible, I prefer switch statements to long if-else-if chains. (For me they are much easier to read and understand, logically).

More info:

https://www.geeksforgeeks.org/switch-vs-else/

https://teamtreehouse.com/library/switch-statement

One more thing, logic, in general, can be very confusing at times, so don't feel bad. I struggle to get logic correct all the time!?! (Often, ands and ors and nots need to be used exactly THE OPPOSITE of what seems intuitive!?! LOL)

I have found, however, that it can help to actually say the test condition OUT LOUD to think it all the way through!?!!

I hope that helps.

Stay safe and happy coding!

Thank you very much, that worked and i like the way you described how to think it through.