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 trialMarvin Deutz
8,349 PointsNew var
Why would you make separate variables for guess > randomNumber and guess < randomNumber?
I chose to solve it like this and find my solution quite a bit cleaner to be honest.
if (parseInt(guess) === randomNumber) {
correctGuess = true
} else if (parseInt(guess) < randomNumber) {
guess = prompt("The number is bigger than your guess");
if (parseInt(guess) === randomNumber) {
correctGuess = true;
}
} else if (parseInt(guess) > randomNumber) {
guess = prompt("The number is smaller than your guess");
if (parseInt(guess) === randomNumber) {
correctGuess = true;
}
}
1 Answer
Eric M
11,546 PointsHi Marvin,
There are many areas where code in the basic courses for each language is not the cleanest or most DRY solution. The goal is to make the language concepts and general programming concepts easy to explain, it's important to have a strong base so that later things like style and abstraction will make sense. In this case Dave is being extra clear with his nested blocks so as to not cause confusion and to draw focus to the different block scopes.
I like your refactor - perhaps the branches could be simplified further by placing this logic within a function that returns a boolean? We could then use that boolean as a while loop conditional to keep the game running until the player guesses correctly.
Cheers,
Eric