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

Brenda Butler
.a{fill-rule:evenodd;}techdegree seal-36
Brenda Butler
Front End Web Development Techdegree Graduate 18,839 Points

Does anyone see any errors or potential issues with this JS code?

I paused the video to see if I could complete the task and I came up with the following code. It's shorter than the way the instructor answered it in the video. It runs just fine. But does anyone see any potential errors with it that I don't see?

var randomNumber = getRandomNumber(10); var guess; var guessCount = 0;

function getRandomNumber( upper ) { var num = Math.floor(Math.random() * upper) + 1; return num; }

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

document.write("You guessed it! The random number was " + randomNumber + " and it took you " + guess + " tries to get it right. Well done!");

1 Answer

Steven Parker
Steven Parker
216,719 Points

You probably want to use "guessCount" instead of "guess" when reporting the number of tries.