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

Teacher Russell
Teacher Russell
16,872 Points

do...while

Oh my, I was just being nostalgic and revisiting these exercises of long ago. This code runs here, but never end. No match. Originally I had done this from memory in my text editor using es6 syntax and had problems. For one, it said prompt was undefined. I came back here to test it on Treehouse, and something's still not right.

https://w.trhou.se/6vsp9csgj1

1 Answer

Steven Parker
Steven Parker
210,423 Points

:mailbox_with_mail: Hi, I got your request message.

The "prompt" method should be a built-in available in any browser. Did you try running in a server-side JavaScript engine like Node.js?

But the issue with the code is due to the handling of the guess:

  guess = prompt('I am thinking of a number between 1 and 10. What is it?');
  guess += 1;

So if the input is a string of digits between 1 and 10, the next line tacks on another "1" digit making it 11, 21, 31, ...etc up to 101. Since none of these will ever match a random number between 1 and 10, the loop never ends.