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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

I've solved it differently, maybe easier? Are there advantages to the official solution that I miss?

var randomNumber = getRandomNumber(10, 1); console.log(randomNumber);
var attempts = 0;

//random number generator
function getRandomNumber (upper, lower) {
    return Math.floor(Math.random() * (upper - lower + 1)) + lower;
}

do {
    var guess = parseInt(prompt('Guess a number between 1 and 10.'));
    attempts += 1;
} while (guess !== randomNumber)

document.write('That\'s right! The number was ' + randomNumber + '</br>It took you ' + attempts + ' attempts to get it right.')

3 Answers

Steven Parker
Steven Parker
230,274 Points

The use of a boolean for loop control is a common technique that is useful when the logic is more complex. But as you discovered, it's not really needed for this simple example.

So yes, your optimization is valid, and evidence of your learning progress and grasp of the material. Good job! :+1:

All right, thanks!

Jay Dacosta
Jay Dacosta
5,913 Points

I literally wrote it the exact same way minus the use of a "lower" argument for the random number generator. Good job!