Web Analytics Made Easy -
StatCounter random number game problem - CodingForum

Announcement

Collapse
No announcement yet.

random number game problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • random number game problem

    I am trying to script a game were a player chooses a number between 1-100.

    A random number generator gives a number.
    The player has up to 7 tries.

    I have scripted the number generator like this

    <script language="JavaScript">
    counter = 0
    while (counter < 7) {
    var num = Math.round(100 * Math.random());

    document.write("<br>Random number between 1 and 100: <b>" +num+ "</b>.")
    counter++
    }

    </script>

    Can anyone please help in how the player inputs the number and then it is checked to see if both numbers match?

    All help much appreciated

  • #2
    I don't know how far you want to take the game but this should get you started

    <script language="JavaScript">
    counter = 0

    function playme(){
    if(document.f1.t1.value==""){return}

    counter++

    player=document.f1.t1.value
    if(document.f1.t1.value>100){
    document.f1.t1.value=100
    }

    var num = Math.round(Math.random()*100);
    document.getElementById("display1").innerHTML=num

    if(num==player){
    document.getElementById("display2").innerHTML="You Win"
    }
    else{
    document.getElementById("display2").innerHTML="You Lose"
    }

    if(counter==7){
    alert("You have now had "+counter+" attempts")
    }

    }
    </script>

    <div id="display1">&nbsp;</div>
    <div id="display2">&nbsp;</div>

    <form name="f1">
    <input type="text" value="" name="t1" size=3 maxlength=3>
    <P><input type="button" value="play" onclick="playme()">
    </form>
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

    Comment


    • #3
      My thanks.
      It gives me a starter.

      The confusing thing was where to put the counter in the script.

      Comment

      Working...
      X