Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
/* Get Programming with JavaScript
 * Listing 12.05
 * Higher or Lower
 */
var getGuesser = function () {
  var secret = Math.floor(Math.random() * 10 + 1);
  return function (userNumber) {
    if (userNumber === secret) {
      return "Well done!";
    } else {
      if (userNumber > secret) {
        return "Too high!";
      } else {
        return "Too low!";
      }
    }
  };
};
var guess = getGuesser();
/* Further Adventures
 *
 * 1) Run the program
 *
 * 2) Play the game a few times.
 *    e.g. guess(2)
 *
 * There are three blocks of code covering the
 * three outcomes for a guess:
 * too low, too high or correct.
 *
 * 3) Update the second condition to
 *    (userNumber < secret)
 *
 * 4) Update the messages to match the
 *    new condition.
 *
 * 5) Swap the first and second conditions so
 *    the second condition uses the strict
 *    equality operator.
 *
 * 6) Update the messages to match the
 *    new condition.
 *
 */
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
jrlarsenpro
0viewers