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

Seth Cardoza
seal-mask
.a{fill-rule:evenodd;}techdegree
Seth Cardoza
Front End Web Development Techdegree Student 2,881 Points

I feel like my code meets all requirements for this quiz but I'm not sure why it won't let me pass.

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' ||  money < 10 ) {
  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

Steven Parker
Steven Parker
229,744 Points

:warning: Note: This challenge isn't checked very thoroughly, you can get a false positive with some significant logic errors.

One approach to solving logic issues is to try translating the conditions into words and see if they match the messages:

if ( money > 10 && today === 'Friday' ) {         // "Today is Friday, and money is more than 10"
  alert("Time to go to the theater.");    
} else if ( money >= 50 && today === 'Friday' ) { // "Today is Friday, and money is 50 or more"
  alert("Time for a movie and dinner.");    
} else if ( today !== 'Friday' ||  money < 10 ) { // "Today is NOT Friday, OR money is less than 10"
  alert("It's Friday, but I don't have enough money to go out.");  // <-- message does not match!

Also note that while the logic of the first two tests does match the messages, the tests needs to be done in the opposite order. Otherwise, since 50 is more than 10, it will tell you to go to the theater when you have $50 but it should say you can have a movie and dinner. This is one of the things the challenge doesn't actually check for.

Yet another one is that one of the two tests with the value 10 should be an "or equals" test, otherwise if you have exactly $10 you can get an incorrect result.

Steven Parker
Steven Parker
229,744 Points

:see_no_evil: Read this section only after you successfully pass the challenge.

Personally, I would code something like this using nested conditions instead of logical combining, it takes a couple more lines but I think it's easier to read and check for correct logic:

if ( today === 'Friday' ) {
  if ( money >= 50 ) {
    alert("Time for a movie and dinner.");
  } else if ( money >= 10 ) {
    alert("Time to go to the theater.");
  } else {
    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.");
}