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

Why do you need to have separate variables - answer1, answer2, answer3 etc. I used answer (just kept getting updated

Why do you need to have separate variables - answer1, answer2, answer3 etc. I used var answer = prompt('What season is it? [summer]'); if (answer.toLowerCase() === 'summer') { score += 1; } var answer = prompt('What day is it? [friday]'); if (answer.toLowerCase() === 'friday') { score += 1; } var answer = prompt('What color is the sky in November? [gray]'); if (answer.toLowerCase() === 'gray') { score += 1; } var answer = prompt('What year is it? [2016])'); if (parseInt(answer) === 2016) { score += 1; }answer (just kept getting updated with each prompt response)

1 Answer

Steven Parker
Steven Parker
229,744 Points

For this particular example, re-using a variable or using separate ones does not make a difference.

However, using separate variables allows an opportunity for program evolution where the individual answers might be referenced later on. Once the answer variable is re-used, the possiblity of accessing the answer given to an earlier question is lost.

NOTE :point_right: A variable should only be declared (using "var") once.

You can re-assign it as much as you like but it should not be declared again.