Nesting If statement to a while statement : while « Statement « JavaScript Tutorial






<html>
<head>
<title>Number Guesser</title>
<script>
var guess = 50;
var target = 0;
var turns = 0;
var msg = "";

target = Math.floor(Math.random() * 100) + 1;

guess = eval(prompt("What is your guess?", guess));

while (guess != target){
  turns++;
  if (guess > target){
    guess = eval(prompt (turns + ".  Too high!! Next guess?", guess));
  }

  if (guess < target){
    guess = prompt (turns + ".  Too low!! Next guess?", guess);
  }

  if (guess == target){
    msg = "YOU WIN!!! \n";
    msg += "It took you ";
    msg += turns + " turns! ";
    alert (msg);
  }
}

</script>
</head>








3.6.while
3.6.1.while loop
3.6.2.Use integer variable to control the while loop
3.6.3.Check the loop counter for while loop
3.6.4.Use while loop to output all elements in an array
3.6.5.Use Do while loop to reverse a string
3.6.6.Nesting If statement to a while statement
3.6.7.Using While loop to check user input