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

Andy Goodman
Andy Goodman
14,207 Points

Is there a problem with this code?

I tried to do the code before it was shown to me and did the 'do while' loop as follows:

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

This seemed to work absolutely fine. However, Dave's code is longer . So I am wondering, is there an issue with the way I have done it? Or is it fine and Dave is just trying to show a different way of escaping a loop?

2 Answers

Steven Parker
Steven Parker
231,268 Points

Dave seems to also be demonstrating the management of a boolean state variable. While this technique can be useful in more complex programs, it seems unnecessary for this simple loop.

Your solution of directly testing the exit condition in the loop is more concise; and in my opinion, perfect for this particular situation.

Good job! :+1:

Andy Goodman
Andy Goodman
14,207 Points

Ok, great, thanks. I expect he was just demonstrating another way of doing it, but using it in a simple loop so it is easier to understand.