Results 1 to 9 of 9

Thread: need help with guess this number program

  1. #1
    Join Date
    Oct 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with guess this number program

    I need help with this Guuess a number program> Its probly easy for for you but I'm brand new at this javascript stuff. Antways here what i got make work and what I have got so far.


    Guess a number program- The user will pick a number from 1 to 1000. Your program should guess the user number in ten tries or less.After each guess the user will specify if the guess is low or high or correct.


    This what I have so far.

    HTML Code:
    <html>
    <head></head>
    <title>Guess your interger</title>
    
    <script language = "javascript">
    	
    function ShowValues{
    
    Var highnum=1000;	
    var lownum=0;
    var guessnum =(lownum+highnum)/2;
    var count=0;
    
    	
    	
    
    	if (document.myform.High.Checked)
    	{highnum=guessnum} 
    	
    	
    
    	
    	if (document.myform.Low.Checked){
    	lownum=guess}
    	
    	guessnum=(lownum+highnum)/2
    	count++
    
    myform.guessbox.value=guess
    myform.counter.value=counter
    
    	
    	
    }
    
    </script>
    
    <body>
    
    <H1><bold><center> Guess Your Integer</h1>
    <h3> Think of an interger between 1and 1000(Don't tell anyone!)<br>
    
    The computer will guess you number in ten guesses or less<br><br><br>
    Check high checkbox if our guess was to high
    <br>
    Check Low checkbox if our guess was to low
    <br>
    Press Guess button to start game.
    <br>
    <br>
    
    <br>
    
    <Form name = "myform">
    
    <input type="button" value="Guess"onclick="ShowValues()">
    <p>Low<INPUT TYPE = "Checkbox" name = "Low">
    <p>High<INPUT TYPE = "Checkbox" name = "High">
    <p>Our guess is<input name = "guessbox" type="text">
    <p>We have made<input name= "counter" size="8">Guesses 
    
    
    
    
    </form>
    </head>
    </body>
    Last edited by tech_support; 10-14-2007 at 02:33 AM.

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    To generate a random number, use Math.random()

    Math.random() creates a number from 0 (inclusive) to 1 (exclusive). Symbolically, the range is [0, 1)

    Math.ceil(number) rounds a number up to the nearest integer.

    Math.floor(number) rounds a number down.

    Code:
    var num = Math.ceil( Math.random() * 1000 );  //A random integer within [1, 1000]
    ----

    About your HTML...
    -The title tag belongs within the head tag.
    -Your script tag should look like
    HTML Code:
    <script type="text/javascript">
    -The script tag should either be within a head or a body tag.
    -You have a </head> at the end of your document that should not be there.
    -You should close your html tag at the end of your document too.
    Last edited by Trinithis; 10-14-2007 at 12:42 AM.
    Trinithis

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's been long enough that you've probably done it by now anyway, so I guess it's OK for me to show off:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>Number Guesser</title>
        <script type="text/javascript">
          var Functional = {
            Operator: {
              add: function(a, b) {
                return a + b;
              }
            },
            reduce: function(f, a, initial) {
              var t = initial || 0;
              for(var i = 0; i < a.length; ++i)
                t = f(t, a[i]);
              return t;
            }
          };
    
          Math.mean = function() {
            return Functional.reduce(Functional.Operator.add, arguments) / arguments.length;
          };
    
          function GuessGame(upperBound, lowerBound, maxGuesses) {
            this.ub = upperBound || 1001;
            this.lb = lowerBound || 1;
            this.mg = maxGuesses || 1000;
            this.breakPlay = false;
          }
    
          GuessGame.HIGHER = 1;
          GuessGame.LOWER = 2;
          GuessGame.CORRECT = 3;
    
          GuessGame.prototype = {
            playBlocking: function() {
              var d;
    
              for(var i = 0; i < this.mg; ++i)
                if(!this.breakPlay && (d = this.tryGuessBlocking()))
                  this.updateBounds(this.getGuess(), d);
                else break;
    
              if(this.breakPlay && this.ub !== this.lb)
                this.fail();
            },
            getGuess: function() {
              return Math.floor(Math.mean(this.ub, this.lb));
            },
            updateBounds: function(num, direction) {
              if(direction === GuessGame.HIGHER)
                if(num < this.ub)
                  return (this.ub = num) ? true : true;
                else {
                  alert(num + " < " + this.ub);
                  return false;
                }
              else if(direction === GuessGame.LOWER)
                if(num > this.lb)
                  return (this.lb = num) ? true : true;
                else return false;
              else if(direction === GuessGame.CORRECT) {
                this.succeed(num);
                return (this.ub = this.lb = num) ? true : true;
              }
            },
            tryGuessBlocking: function() {
              var h;
              do
                h = (h = prompt("I guess " + this.getGuess() + ".  Is this [h]igher, [l]ower, or [c]orrect?")) ? h.charAt(0).toLowerCase() : h;
              while(h !== null && (h.length !== 1 || "hlc".indexOf(h) === -1));
    
              if(h === null)
                return null;
              else return ({h: GuessGame.HIGHER,
                l: GuessGame.LOWER,
                c: GuessGame.CORRECT})[h];
            },
            succeed: function(n) {
              this.breakPlay = true;
              alert("The answer is " + n);
            },
            fail: function() {
              this.breakPlay = true;
              alert("I failed to find your answer.");
            }
          };
        </script>
      </head>
      <body>
        <div>
          <input type="button" onclick="(new GuessGame()).playBlocking();" value="Play">
        </div>
      </body>
    </html>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    It's been long enough that you've probably done it by now anyway, so I guess it's OK for me to show off:
    Long enough? It was only yesterday! He/She probably hasn't even cracked the book yet.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well, considering that it was probably last minute before s/he asked anyway...
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Offhand, I'm not really sure exactly what time of day Sunday it is over there, later I know. But when you posted your solution it was well before late Sunday night, the time when all true homework slackers get down to it, if they are going to do it at all. Over here, there's still time for a pick up game of basketball, or whatever before anyone needs to worry seriously about homework.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Heh, fair enough I won't take it down now since I'd just lose it. I suspect the teachers would be suspicious if s/he handed in my work anyway, though, judging by the previous attempt.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Oct 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    To be honest I'm not a homework slacker this assignment was already turn in as is I got a 80 for trying and at least getting some things right. However my professor doesn't tell us what we did wrong and I personaly want to get this program to work.
    Thanks again for the Help
    Vinnie

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by alesv View Post
    my professor doesn't tell us what we did wrong
    Bad professor, bad, bad . . .
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •