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
Elizabeth Eymann
12,209 PointsDo while loops
HI!
Can anyone help? I wrote the code in the video for "do while loops". When I preview my work, everything seems to work. I get the prompt boxes asking me to guess a number from 1 to 10. But after guessing multiple times, the message prints to the screen "it took you 0 attempts to guess the number." I know that's not true. It took me multiple attempts! Can anyone see what I'm doing wrong here? Here is a screenshot of my workspace. Thanks!
1 Answer
andren
28,558 PointsYou don't increase the guessCount variable within your loop. You set it to 0 at the start of the program and then just print it out at the end. JavaScript won't do something you haven't explicitly instructed it to do, so if you don't ask it to increase the value in that variable then it won't do so on its own.
You can fix this issue by simply placing the code guessCount++ inside your loop like this:
do {
guess = prompt('I am thinking of a number between 1 and 10. What is it?');
if (parseInt(guess) === randomNumber) {
correctGuess = true;
}
guessCount++;
} while ( ! correctGuess )
Placing ++ at the end of a variable name is a shortcut for increasing it by 1.