The Computer is thinking of a number between 1 and 10
Press Reload to Play Again

Here's the thinking: In this type of script, there can only be three results. Too high. Too low. Right on. That means that each time the player takes a shot, one of the three will be enacted.

IF/ELSE is very this-or-that in its thinking. Here you won't ever need an else because you know there are only three outcomes, one of which will be enacted each time the game is played.

Thus...you only need three IF statements.

Please note in the code that the < and > commands are used. The mean just what they always have to you. I always think of it as the aligator mouth wants to bite the bigger item. That's how I keep the form straight. Here's the code:

The Script

<html>
<head>
  <SCRIPT type="text/javascript">
    function rand()
      {now=new Date()
       num=(now.getSeconds())%10
       num=num+1
      }
    function guessnum()
      {guess=prompt("What is your guess?")
      if (eval(guess) == num)
         {alert("YOU GUESSED MY NUMBER!!")
         }
      if(eval(guess) > num)
 {alert("Too high!  Press the button to guess again.")}
      if(eval(guess) < num)
 {alert("Too low!  Press the button to guess again.")}
     }
  </script>
<body bgcolor="white" onLoad="rand()">
  <h2>
  The Computer is thinking of a number between 1 and 10<br>
  Press Reload to Play Again</h1>
  <form name="myform">
    <input type="button" 
	value="Guess the number" 
	name="b1" 
	onClick="guessnum()">
  </form>
</body>
</html>

Close this window to return to JavaScript Primer #22