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

website crashes while trying another strategy!

i tried using another strategy to achieve this goal. instead of using the same function in the while loop. instead, i created another function with different name having the same mathematical equation and stored it in a new variable and set the while loop to measure the values of both variables. when the result is true for (function2 !== function1), function 2 is then recalled and then the loop continues till it supposedly matches the value of that of function1. the only problem is, when i preview the code, it loads for tremendous amount of time and then the browser crashes. any idea on to why that's happening?

jag
jag
18,266 Points

Post the code.

here is the code:

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

function secRandom (lower) { return Math.floor( Math.random() * lower ) + 1; }

var upper = 10000; var lower = 10000; var guess = secRandom(lower); var randomNumber=original(upper); var counter= 0; while (guess !== randomNumber ) {

secRandom(lower); counter+=1;

} document.write("it took the computer " + counter + " number of times to guess the number right. "); document.write("The correct Number is " + randomNumber + ".");

Update: when secRandom (lower) is called, the browser crashes, if "guess" var is reassigned by the same function , written like this : "guess=secRandom(lower);" the code runs smoothly. would you care to explain?

2 Answers

jag
jag
18,266 Points

Adding from what Shane mentioned. Yes, the while loop you currently have is an infinite loop. When you call secRandom() it won't be assigned to the guess inside the while condition. It's out of scope and therefore won't ever end the loop.

computerGuessLoop.js
console.clear()
var upper = 1000000,
     lower = 1000000,
     counter = 0,
     randomNumber = Math.floor( Math.random() * upper ) + 1,
     guessCorrect = false,
     i=0;

while(guessCorrect == false){
    i++
    var guess = Math.floor( Math.random() * lower ) + 1;
  if(guess == randomNumber){
    guessCorrect = true; // Breaks loop
    console.log("Number of tries:" + i)
    console.log("Computer Guess: " + guess +" & RandomNumber: " + randomNumber)
  }
}

To see it in action JSFiddle

Shane Oliver
Shane Oliver
19,977 Points
//You're stacking your browser with this infinite loop. It never escapes
//you need to assign  a new random number to guess to give it a chance of being equal to randomNumber
while (guess !== randomNumber ) {

   secRandom(lower); counter+=1;

}