Javascript problem

Discussion in 'Programming General' started by Eur0, Nov 14, 2012.

Javascript problem
  1. Unread #1 - Nov 14, 2012 at 11:32 AM
  2. Eur0
    Joined:
    Jul 6, 2009
    Posts:
    213
    Referrals:
    0
    Sythe Gold:
    4
    Lawrence

    Eur0 Active Member
    $25 USD Donor New

    Javascript problem

    How would you make a page with a javascript function that rolls a dice or two six sided dices a undefined amount of times then displays the average in a seperate box can anyone help me with this?
     
  3. Unread #2 - Nov 14, 2012 at 11:40 AM
  4. Terrankiller
    Joined:
    May 7, 2005
    Posts:
    1,286
    Referrals:
    1
    Sythe Gold:
    1

    Terrankiller Ex-Administrator
    Retired Administrator Visual Basic Programmers

  5. Unread #3 - Nov 28, 2012 at 10:47 AM
  6. printingray
    Joined:
    Oct 9, 2012
    Posts:
    37
    Referrals:
    0
    Sythe Gold:
    0

    printingray Member

    Javascript problem

    I think this might help you

    01 <html>
    02 <head>
    03 <script type = "text/javascript">
    04
    05 //player1 variables
    06 var dice1 = ["1", "2", "3", "4", "5", "6"]; //string for concatenation
    07 var dice2 = ["1", "2", "3", "4", "5", "6"];
    08 var score1; //concatenation
    09 var total1 = 0; //total number of wins
    10
    11 //player2 variables
    12 var dice3 = ["1", "2", "3", "4", "5", "6"];
    13 var dice4 = ["1", "2", "3", "4", "5", "6"];
    14 var score2;
    15 var total2 = 0;
    16 var rnum = 0; //round number for table from 1 till 5
    17 var decicsion1; //if player1 won
    18 var decision2; // if player2 won
    19 var round = 0; //the counter variable to limit the button to be clicked 5 times
    20
    21
    22 //starting table headers
    23 document.writeln("<table border = \"1\"><thead>");
    24 document.writeln("<th>Round</th>" + "<th>Player 1</th>" + "<th>Player 2</th></thead><tbody>"); //1st row
    25
    26 if (round<=5) // here starts the counter to limit and let the function only work 5 times for the button
    27 {
    28 //IDK what to put here return 0; does nothing obviously. I want it to close after the button has been clicked the 5th time.
    29
    30 function play()
    31 {
    32 //player1
    33 dice1 = Math.floor( 1 + Math.random() * 6 ); //this should randomize the string values or generate new numbers.
    34 dice2 = Math.floor( 1 + Math.random() * 6 );
    35 score1 = dice1 + dice2; //concatination I've seen this method there is another methods like score1 = dice1.concat(dice2); both do not work
    36
    37 //player2
    38 dice3 = Math.floor( 1 + Math.random() * 6 );
    39 dice4 = Math.floor( 1 + Math.random() * 6 );
    40 score2 = dice3 + dice4;
    41
    42
    43 //total wins
    44 /*what I want this to do is add 1 to the winner so player1, 2 start at 0,0
    45 then who ever wins its 0,1 then 1,1 or 0,1 like a soccer scoring board*/
    46 if (score1>score2)
    47 {
    48 total1 += 1;
    49 total2 += 0;
    50 }
    51 else{
    52
    53 total1 += 0;
    54 total2 += 1;
    55 }
    56 //desicion of who won
    57 if (total1>total2)
    58 {
    59 decision1= document.write("Winner");
    60 decision1= document.write("Loser");
    61 }
    62 else{
    63 decision1= document.write("Loser");
    64 decision1= document.write("Winner");
    65 }
    66
    67
    68
    69
    70 // The 3x8 table that has all the results
    71 rnum = Math.floor(5 * Math.random()); //round number
    72 document.writeln("<tr><td>round</td><td>"+rnum+"</td><td>"+score1+"</td><td>"+score2+"</td><tr>");// 1-5 rows
    73 document.write("<tr><td>total</td><td>"+total1+"</td><td>"+total2+"</td><tr>"); //6th row
    74 document.write("<tr><td>Winner</td><td>"+decision1+"</td><td>"+decision2+"</td><tr>"); //7th row
    75 }
    76 } //closeing of the if-statment
    77
    78 </script>
    79 </head>
    80 <body>
    81 <form action="">
    82 <input type="button" value="Roll Dice" onclick="play()">
    83 </form>
    84 </body>
    85 </html>
     
< Where to start? | Looking for a programmer, easy-ish projects >

Users viewing this thread
1 guest


 
 
Adblock breaks this site