Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,325 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.