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

Isn't he over complicating it by using an if statement? Isn't this code better?

Here is my code and it seems to be working fine...

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

document.write('Yes! ' + guess + ' was the right number!'); document.write(' It took you ' + guessCount + ' tries to get it right!');

1 Answer

Steven Parker
Steven Parker
229,771 Points

:point_right: You have effectively optimized the program.

It could be that the technique of setting a boolean value to indicate completion was being demonstrated only because it can be useful in more complicated loops.