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 A Closer Look at Loop Conditions

Alex Lang
Alex Lang
1,110 Points

Why won't this work?

var upper = 10000;
var randomNumber = getRandomNumber(upper);
var guess;
var attempts = 0;


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

while (guess !== randomNumber ) {
guess = getRandomNumber( upper );
attempts += 1;  
}
document.write("<p>The random number was " + randomNumber + "</p>");
document.write("<p>It took the computer " + attempts + "attempts to get it right.</p>");

It seems like it works to me. What does it not do?

Alex Lang
Alex Lang
1,110 Points

When I preview it, nothing pops up.

5 Answers

The source for your script is app.js. But the code is in while.js. Make the following change in index.html

<script src="while.js"></script>

I'm having the same problem and i added the script anyone know what's wrong?

Steven Parker
Steven Parker
211,016 Points

If the answer here didn't resolve your issue you may have a different cause. Start a fresh question where you can share your own code and we can take a look. Remember to use Markdown formatting.

I tried EVERYTHING....still does not WORK! So frustrated!

Steven Parker
Steven Parker
211,016 Points

Start a fresh question where you can show your own code, I'll bet someone can help!

Steven Parker
Steven Parker
211,016 Points

This code does work, but remember when dealing with random numbers, the specific results you get will vary from those in the video. In fact, if you try it more than once, you will notice that the number of attempts will be different each time.

Steven Parker
Steven Parker
211,016 Points

Looks like Kris beat me to it, but you can see that snapshot made the answer possible.

Steven Parker
Steven Parker
211,016 Points

Would the person who downvoted this answer please leave a comment and let me know what I could do to improve it? Thanks!

Alex Lang
Alex Lang
1,110 Points

When I preview it, nothing pops up.

Steven Parker
Steven Parker
211,016 Points

Are you using the workspaces? If so, you could make a snapshot of your workspace and post the link to it here. Then we could replicate your issue.