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 OR operator please help

Something's wrong with this script. The value in the variable money is only 9. But if you preview this script you'll see the "Time to go to the theater" message. Fix this script so that it correctly tests the money and today variables and prints out the proper alert message: "It's Friday, but I don't have enough money to go out"

var money = 9; var today = 'Friday';

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

My error says ---> Bummer! Hmm. Looks like there is at least one logical OR operator -- that's the || symbols. Those test if just one of the condition are true.

3 Answers

You're using the OR operator throughout. For what you want to do I think you should be using the AND operator.

The reason you are getting the output "Time to go to the theater" is because your first if statement tests to see if you have $100 or more OR if it's Friday. As today is defined as "Friday" it's going to evaluate to true (remember - if one side of an OR statement is true the whole statement evaluates to true).

What I think you want is

if ( money >= 100 && today === 'Friday' )

so that if you have less than $100 or it's not Friday then it evaluates to false and the program moves onto the next if statement.

It looks as though all your if statements will need changing and I'd imagine you probably want the last one to be

  if ( money < 10 && today === 'Friday' )

to get the desired outcome.

Steven Parker
Steven Parker
243,657 Points

The "Bummer" line gives you a clue. The original code is only testing to see if either condition is true.

:point_right: To check if both conditions are true, you need the && ("and") operator.

hey guys thanks for helping :D