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 (Retired) Making Decisions with Conditional Statements Super Conditional Challenge

Zack Taylor
Zack Taylor
9,021 Points

Need to figure out this code... I want it to display .. its friday, but i dont have enough money to go out sequence.

I want it to display .. its friday, but i dont have enough money to go out sequence. The money = 9 but when refreshing the page it seems to think that the money is >= 100 ???

just cant seem to figure it out. I bet its just something like a || operator but i cant seem to work it out.

can someone help please ta.

code :

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 ( 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.");
}
Hugo Paz
Hugo Paz
15,622 Points

You are using an or operator. When javascript evaluates the first expression

if ( money >= 100 || today === 'Friday' ) { alert("Time to go to the theater");
} 

It returns true because an or operator always returns true if one of the conditions is true.

What you want to use here is the && operator like this

if ( money >= 100 && today === 'Friday' ) { alert("Time to go to the theater");
} 

The same applies to the rest.

anguswhiston
anguswhiston
17,225 Points

Your first line says "If money is equal to or greater than 100, OR, the day is friday then give an alert that says "It's time to go to the theatre"

So your code runs and it checks the money value and it's less than 100, however it moves onto the next bit and checks if the day is Friday. The day IS friday so it alerts it's time to go to the theatre.

I think you want to use the && notation where you've put the ||

Then your alerts will only run if they money is ok AND the day is Friday

7 Answers

Yes I have done the challenge and passed it -_- anyways here is how it is suppose to be. I gave a thorough explanation up there as well.

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 ( 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.");
}

Cool... Gloria

As mentioned earlier this part is the tricky part. The "OR" operator will result to true if one of the statements in both sides is true... because it is indeed Friday..... even if money is not more or equal than 100 the 1st statement will always run.

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 ( 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.");
}

Now lets say you want to make sure both cases are true in the 1st if statement by using the "AND" operator symbolized as "&&", before alerting what is in the braces.

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 ( 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.");
}

the 1st if will result to false, since it is Friday but then again, the money is not equal or over 100 but 9. So it will go check the next if statement. The money is not equal or over 50 BUT it is indeed Friday, so since the || operator needs only one of the two statements to be true for it to result to true it will show the 2nd alert "Time for a movie..." Now if again you want to make that alert to show only when both cases are true you will have to change it to an && statement. So if you want to have certain alert for the money being less than 9 AND the day being Friday you will have to make sure both statements result to true. The same applies for the other if statement.

Now I want to put an emphasis on this statement

} else if ( today !== 'Friday' ) {
  alert("It's Friday, but I don't have enough money to go out");   
} 

that statement says if today isn't equal to Friday, it should alert "It's Friday, but I don't have enough money to go out".

I hope this explains what is happening better, and helps you come up with the solution.

Roscoe Coney
Roscoe Coney
13,124 Points

I think something is wrong with this Challenge, I have change the string in the initial if statement and it STILL returns, "Time to go to the theater". No matter which variables i change it produces this message, I even deleted the ENTIRE "script.js" and it produces the "Time to go to the theater" alert, can someone either provide the answer or fix the code?!

Hi Roscoe,

You should not be changing the strings in the alert statements. You want to fix the conditional checks. Gloria has the full solution in her answer.

The second code block show how the logical OR has been changed to a logical AND in the first condition. It also needs to be done for the other 2.

The condition for the last else if also has to be changed so that it matches what the alert is saying.

Roscoe Coney
Roscoe Coney
13,124 Points

Ok, I finally figured it out! Thanks Jason, it was frustrating... trying to figure this out but persistence is key! I just didn't know what I could and could not change, I think this is better suited for a quiz question but that is just my opinion, since u can only edit certain parts. Thanks AGAIN!! on my way to the MOST points in Treehouse!

Great job hanging in there and figuring it out!

Good luck on your quest for the MOST points.

Zack Taylor
Zack Taylor
9,021 Points

This challenge is not going so well. I think I understand whats going. This is my version of the completed challenge....

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 <= 9 || 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.");
}

instead I am getting the message back "Time to go to the theater". Even tho in FSfiddle I am getting the correct response back. That could be "It's Friday, but I don't have enough money to go out".

Why is this not working ???

( money <= 9 || today !== 'Friday' ) ....if that is true... then why print... "It's Friday, but I don't have enough money to go out" also add a semicolon at..." var today = 'Friday';"

Zack Taylor
Zack Taylor
9,021 Points

yeah but surely thats the right answer? it makes sense ? Gloria have you tried this challenge yourself? As in FSfiddle its saying that im correct!!! I am starting to think there is an error your end at treehouse. :/

If you tell me what the code should be... I will copy and paste it to see if it passes. I will then study that code and see my mistakes.

EDITED!!!! I have just had another go...

and this is the code i put in. 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 <= 9 && 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."); }

It has let me pass but the alert box is still saying ... "Time to go to the theater". unsure if this is right. Tho the challenge has let me pass it.

Hi Zack,

I think the preview portion of the code challenge isn't working and it always has that alert.

I know you have the right answer now but I wanted to let you know what could go wrong by adding a money condition on the last else if condition. As can be seen in Gloria's answer, a money condition isn't necessary. If you do put a money condition in then it needs to be money <= 10 so that you don't have any gaps in the money.

Suppose you have exactly $10. With your code above you'll end up falling through to the else and it's going to say that it's not friday, even though it is. You have a gap where you're not covering $9.01 up to $10

The easiest thing to do then is not put in the money condition.

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 ( 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."); }