topical media & game development

talk show tell print

lib-present-course-webtechnology-slides-HighLow.js / js



  // HighLow.js
  
  var thinkingOf;  // Number the computer has chosen (1 through 1000)
  var guess;       // User's latest guess
  
  // Initialize the computer's number
  thinkingOf = Math.ceil(Math.random()*10);
  
  // Play until user guesses the number
  guess = window.prompt("I'm thinking of a number between 1 and 10." +
                        "  What is it?", "");
  while (guess != thinkingOf) 
  {
  
    // Evaluate the user's guess
    if (guess < thinkingOf) {
       guess = window.prompt("Your guess of " + guess + 
                             " was too low.  Guess again.", "");
    }
    else {
       guess = window.prompt("Your guess of " + guess + 
                             " was too high.  Guess again.", "");
    }
  }
  
  // Game over; congratulate the user
  window.alert(guess + " is correct!");
  


(C) Æliens 20/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.