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 Improving the Random Number Guessing Game

Johnny Harrison
Johnny Harrison
4,331 Points

Can we reuse the 'guess' variable?

In my program, I just had the conditional statement overwrite the 'guess' variable instead of making new 'guessLess' and 'guessMore' variables. Is there anything wrong with this approach?

1 Answer

Stamos Bolovinos
Stamos Bolovinos
4,320 Points

Hi Johnny

I think I can answer this question, although I'm quite new to JavaScript, but I do have scripting experience in contact center routing strategies.

Overwriting a variable, if you don't need to keep track of the old value, is very common. A variable can get overwritten many times while a script runs.

The only reason I can think of for using more than one variable in this case, is if you wanted to add reporting at the end and display the two guesses to the user. But even in this case, two variables would do the job (e.g. guessSecond).