...

View Full Version : Need help with Tidying code



dave_UK
03-27-2012, 12:51 AM
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.





<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>





<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>

jmrker
03-27-2012, 02:34 AM
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:


<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>

dave_UK
03-27-2012, 09:28 AM
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 ?

jmrker
03-27-2012, 03:52 PM
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:


<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>

dave_UK
03-27-2012, 09:09 PM
thanks i will play around with this code for a while..

jmrker
03-28-2012, 05:11 AM
thanks i will play around with this code for a while..

You're most welcome.
Happy to help.
Good Luck!
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum