0
   

stuck on javascript assignment

 
 
vmattox
 
Reply Mon 24 Feb, 2014 01:03 pm
i am trying to write a javascript that will roll 2 dice and tally and record what is rolled 36000 times. this is what i have so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Dice Roller</title>

<script type="text/javascript" charset = "utf-8">

function side( ) {

if ( document.getElementById("sides").value < 2 || document.getElementById("sides").value > 100 ) {
alert( "Value you entered is not within range" );

} else if ( isNaN(document.getElementById("sides").value )) {

alert( "Value you entered is not within range" );

} else {

Die(sides);

}
return false;
}


function Die( ){

this.sides = 6;

this.roll = function( ) {

return parseInt((Math.random( ) * 1000) % this.sides) + 1;

}

}


function field_prompt(field_name, prompt) {
var sides = document.getElementById(field_name);

sides.value = prompt;

sides.style.color = "#888";

sides.onfocus = function( ) {

this.style.color = "#000";

if ( this.value == prompt ){

this.value = "";

}

}

sides.onblur = function( ){

if( this.value == "" ){

this.style.color = "grey";

this.value = prompt;

}

}
}

window.onload = function(){

field_prompt("sides", 6)
}



</script>
</head>
<body>

<h1>Dice Roller</h1>
<hr />

Enter a number of sides for the die between 2 and 100.

<form action="" name="dice_roller" id="dice_roller" />
<p><input type="text" name="sides" id="sides" /></p>
<p><input type="button" value="Click to roll the Die!" onclick="side();" /></p>

</form>

<script type="text/javascript" charset = "utf-8">

var d = new Die();




</script>
<h2>Actual Rolls</h2>
<dl>
</dl>
<h2>Percentages of Roll Total</h2>
<dl>
</dl>



</body>
</html>

so everything is working ok except i need to add a way to get the value rolled, how many times its been rolled and the percentage that number is rolled and print it out something like this;
Actual Rolls
2 : 500
3 : 1000
4: 270000

Percentages of Roll Total
2: 1%
3: 36%
4:70%

i hope you get my drift numbers are not accurate
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 679 • Replies: 0
No top replies

 
 

Related Topics

 
  1. Forums
  2. » stuck on javascript assignment
Copyright © 2025 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 02/10/2025 at 09:53:36