JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Krunal Patel
Krunal Patel
1,770 Points

Why does my code crash if I do this way?

var  count  =  0;

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

while ( count !== getRandomNumber() ){
  count++;
}

console.log( getRandomNumber() );

console.log( count );

1 Answer

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 49,975 Points

As count increases the range for guessing the correct random number decreases (between count and 100). Once count is greater than 100 you have an infinite loop.

Krunal Patel
Krunal Patel
1,770 Points

Oh, I see. Thank you for making it clearer to me.