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

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
210,423 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.