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

Running this JavaScript crashes my browser... Any idea why?

I am in the middle of a JavaScript course and am following along in workspaces. However, I have noticed throughout the view videos I have done my browser seems to be running slow whenever I run the javascript. When I run the following code, the browser will freeze up and I have to close it out and restart. Is there something in the code that is making it do that?

var upper = 10000;
var randomNumber = getRandomNumber;
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>");

1 Answer

try with this solution to see if it freezes again :

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

function getRandomNumber() {
  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>");

Unfortunately, that did not work either. It's still doing the same thing.