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

Brenda Butler
seal-mask
.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
229,732 Points

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