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 Review Else If Clauses and JavaScript Comments

Abir Ahmed
PLUS
Abir Ahmed
Courses Plus Student 8,616 Points

Why in the following code the first condition passed so only it's code ran? Can you please explain

var itemToBuy = ''; var savings = 1000; if ( savings > 500 ) { itemToBuy = 'Computer'; } else if ( savings > 200 ) { itemToBuy = 'Phone'; } else if ( savings > 0 ) { itemToBuy = 'Dinner'; } else { itemToBuy = '...still saving...'; }

1 Answer

Joshua Paulson
Joshua Paulson
37,085 Points

Remember, IF and ELSE IF. They are conditional and need to determine what part of the code is true.

Savings = 1000 --- Therefor --- Things to buy = Computer

(ELSE IF) Savings = something less than 500 = One of the other conditions.

More simply, If true = true else if true = false

True is equal to true so it passes and the true equals false doesn't run.

Hope that helps.