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

confused about var itemToBuy

I do not understand why the correct answer to "After this code runs, what is the value of the variable itemToBuy?" is actually the correct answer.

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

you just need to check which condition its true.

var itemToBuy = '';
var savings = 1000;

if (savings > 500) { 
    itemToBuy = 'Computer';
//here is saying if savings is more than 500. then item to buy is computer if its less is going to check the next condition.
// in the savings variable you have 1000 which is more then 500. that means the first condition will run because its true
// if its false then  it will check the next else if.

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

i hope this makes sense