SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation HELP please!!!

    This script is intended to play a numeric guessing game. The script chooses a random number between 0 and 99 and invites the user to enter their guess. It tells them if their guess is too high or too low.

    At the moment the game cannot end as the script cannot tell when the number is guessed. In addition there is no limit upon the number of guesses that the user can make. How do I amend the document so that the user has at most 6 attempts and the feedback area informs them if they have won or lost.

    <HTML><HEAD>
    <TITLE>Guessing Game</TITLE>

    <SCRIPT LANGUAGE="JavaScript">
    var guessNumber = 0
    var guesses = new Array()
    var toGuess = this.parseInt( Math.random() * 99)

    function testGuess() {

    var toShow = ""

    guesses[ guessNumber] = this.guessGame.theGuess.value
    guessNumber++
    for ( var index =0; index < guessNumber; index++ ) {
    if ( guesses[ index] < toGuess) {
    toShow = toShow + guesses[ i] + " too low\n"
    } else {
    toShow = toShow + guesses[ index] + " too high\n"
    } <!-- End if -->
    } <!-- End for -->
    this.guessGame.feedback.value = toShow
    } <!-- End testGuess -->

    </SCRIPT>

    </HEAD>
    <BODY>
    <FORM NAME="guessGame"> <CENTER>
    <H2>Try to guess a number between 0 and 99 by typing your<BR>
    number into the field and pressing the <I>DoIt</I> button</H2>
    <INPUT TYPE=TEXT NAME="theGuess"><P>
    <INPUT TYPE=BUTTON NAME="doIt"
    VALUE="Do It" onClick="testGuess()"><P>
    <TEXTAREA NAME="feedback" COLS=40 ROWS=7></TEXTAREA>
    </CENTER></FORM>
    </BODY></HTML>

    AGAIN A BIG THANX FOR YOUR HELP

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <HTML><HEAD>
    <TITLE>Guessing Game</TITLE>
    
    <SCRIPT LANGUAGE="JavaScript">
    var guessNumber = 0
    var guesses = new Array()
    var toGuess = parseInt( Math.random() * 99)
    var maxGoes = 5
    
    function testGuess() { 
    
    	var toShow = ""
    
    	guesses[ guessNumber] = document.guessGame.theGuess.value
    	guessNumber++
    
    	for ( var index=0; index < guessNumber; index++ ) {
    		if ( guesses[ index ] < toGuess) { 
    			toShow = toShow + guesses[ index ] + " too low.\n"
    		} 
    		else if ( guesses[ index ] > toGuess) { 
    			toShow = toShow + guesses[ index ] + " too high.\n"
    		} else {
    			toShow = toShow + "You got it! Answer was " + toGuess + "\nand you did it in " + guessNumber + " goes.\n"
    		} <!-- End if -->
    		if (index + 1 == maxGoes) {
    			toShow = toShow + "Too bad! You've had your " + maxGoes + " attempts.\nAnswer was " + toGuess + "\n"
    		}
    	} <!-- End for -->
    
    	document.guessGame.feedback.value = toShow
    } <!-- End testGuess -->
    
    </SCRIPT>
    
    </HEAD>
    <BODY>
    <FORM NAME="guessGame" onsubmit="return false">
    <CENTER>
    <H2>Try to guess a number between 0 and 99 by typing your<BR>
    number into the field and pressing the <I>DoIt</I> button</H2>
    <INPUT TYPE=TEXT NAME="theGuess"><P>
    <INPUT TYPE=BUTTON NAME="doIt" VALUE="Do It" onClick="testGuess()"><P>
    <TEXTAREA NAME="feedback" COLS=40 ROWS=7></TEXTAREA>
    </CENTER>
    </FORM>
    </BODY>
    </HTML>
    That's a working version, though it still could be better.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THANX EVER SO MUCH 4 YOUR INPUT


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •