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

more fire
more fire
Courses Plus Student 7,253 Points

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
216,719 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:

Jay Dacosta
Jay Dacosta
3,425 Points

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