Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    6
    Thanked 1 Time in 1 Post

    Need help with Tidying code

    i have a dice game but would like to get, when i click on the Button it loads a new page with the Data. its working fine but

    I would like to use InnerHTML but cant get it to work,

    or even place it in a Table in the same page im working on.


    Code:
        
    <script language="Javascript" type="text/javascript">
      
      var total;
      var total1;
      var totalArray = new Array (6);
      var totalArray = [0, 0, 0, 0, 0, 0];
    var totalArray1 = new Array (6);
     var totalArray1 = [0, 0, 0, 0, 0, 0];
      //total = Math.floor((Math.random() * 6) + 1);
    
      
      
    
    
    for (var count = 1; count <= 10; count = count + 1)
    	{
    
    
      var total = (rollDie()-1);
      
      totalArray[total] = totalArray[total] + 1;
      totalArray1[total1] = totalArray[total1] + 1 /10;
    	}
    	
    	
      function rollDie () {
      
          var total = 0;
    	
    	  
    	       total = Math.floor((Math.random() * 6) + 1);
    	       total1 = Math.floor((Math.random() * 6) + 1 /10);
    return total;
    return total1;
      }
    
    
      function playGame () {
       
       someHTML = "<br>Example</br>";
       document.getElementById("dice").innerHTML += rollDie;
       
      //document.write('<table border="1" cellspacing="1" cellpadding="5">')
       
        //  document.write('<tr>')
       //document.write('<td>1 Was Rolled </td>  ' +  '<td>totalArray[0] +  Times. </td>')
       //document.write('<td>row ' + i + ', column 1</td>')
       //document.write('<td>row ' + i + ', column 2</td>')
       //document.write('</tr>')
       
       
      document.write (' 1 was rolled ' + totalArray[0] + ' times. ' + totalArray1[0] + '%');
    	
    	    
    document.write (' 2 was rolled ' + totalArray[1] + ' times. ' + totalArray1[1] + '%');
      
         
      document.write (' 3 was rolled ' + totalArray[2] + ' times. ' + totalArray1[2] + '%');
      
         
      document.write (' 4 was rolled ' + totalArray[3] + ' times. ' + totalArray1[3] + '%');
      
         
      document.write (' 5 was rolled ' + totalArray[4] + ' times. ' + totalArray1[4] + '%');
      
         
      document.write (' 6 was rolled ' + totalArray[5] + ' times. ' + totalArray1[5] + '%');
      
      
     	// document.write (' 2 was rolled ' + totalArray[1] + ' times.');
    	
    	 
    // 	 document.write (' 3 was rolled ' + totalArray[2] + ' times.');
    	
    	 
     //	 document.write (' 4 was rolled ' + totalArray[3] + ' times.');
    	
    	 
     	// document.write (' 5 was rolled ' + totalArray[4] + ' times.');
    	
    	 
     	 //document.write (' 6 was rolled ' + totalArray[5] + ' times.');
    	
    	 
     	 document.close ();
      }
    
    //document.write('</table>')
    	</script>
    </head>

    Code:
    <h3>Dice Roll</h3>
    				<p>&nbsp;</p>
    				<p>&nbsp;</p>
    	
        <form 
      action="" 
      method="get" 
      name="dieForm">
      
      <input 
     	 name="rollButton" 
     	 type="button"
     	 value="Roll the Die"
     	 onclick = "playGame();">
    	</form>
        
    <div class="dice">
    
    
    </div>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,092
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    You have a lot of unnecessary code, a number of syntax errors
    and you should check the 'error console' frequently.
    Compare this version to your attempt and note the numerous modifications:
    Code:
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    function rollDie () {
      var total1 = Math.floor((Math.random() * 6) + 1);
      var total2 = Math.floor((Math.random() * 6) + 1);
      return total1 + ' : ' +total2;
    }
    function playGame () {
       someHTML = "<br>Example</br>";
       document.getElementById("dice").innerHTML += rollDie()+'<br>';
    }
    </script>
    </head>
    <body>
    <h3>Dice Roll</h3>
    <p>&nbsp;</p>
    <form action="" method="get" name="dieForm" onsubmit="return false">
    <input name="rollButton" type="button" value="Roll the Die" onclick = "playGame()">
    </form>
    <div id="dice"></div>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    6
    Thanked 1 Time in 1 Post
    With the Code i have it place them in a Array and out puts the
    Total of

    all Numbers at once E.G

    1 was rolled 20 times 19% 2 was rolled 100 times etc..

    with InnerHTML how can i get this done ?

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,092
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Quote Originally Posted by dave_UK View Post
    With the Code i have it place them in a Array and out puts the
    Total of

    all Numbers at once E.G

    1 was rolled 20 times 19% 2 was rolled 100 times etc..

    with InnerHTML how can i get this done ?
    Should not be a problem to modify:
    Code:
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    function rollDie () {
      var total1 = Math.floor((Math.random() * 6) + 1);
      var total2 = Math.floor((Math.random() * 6) + 1);
      return total1 + ' : ' +total2;
    }
    function playGame () {
       someHTML = "<br>Example</br>";
       var tarr = [];
       for (var i=0; i<20; i++) { tarr.push(rollDie()); }
       document.getElementById("dice").innerHTML = someHTML + tarr.join('<br>');
    }
    </script>
    </head>
    <body>
    <h3>Dice Roll</h3>
    <p>&nbsp;</p>
    <form action="" method="get" name="dieForm" onsubmit="return false">
    <input name="rollButton" type="button" value="Roll the Die" onclick = "playGame()">
    </form>
    <div id="dice"></div>
    </body>
    </html>

  • Users who have thanked jmrker for this post:

    dave_UK (03-27-2012)

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    6
    Thanked 1 Time in 1 Post
    thanks i will play around with this code for a while..

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,092
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Thumbs up

    Quote Originally Posted by dave_UK View Post
    thanks i will play around with this code for a while..
    You're most welcome.
    Happy to help.
    Good Luck!


  •  

    Posting Permissions

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