Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Calculation

Ok, im making a calculator type thing for a game and i want to make a table that when you type in the Minimum damage and max damage it adds those up then divides the answer by 2 and then divides that answer by the speed.

((Min Damage + Max Damage) / 2) / Speed

Then it will insert the answer into the form

• Code:
```<script type="text/javascript">
{
var idsKey=['min','max','speed'];
for(var i=0;i<idsKey.length-1;i++)
{
document.getElementById(idsKey[i]).onkeydown=function(){
document.getElementById('ans').value=compute();
//compute();
}
}
}
function compute()
{
var min=Number(document.getElementById('min').value),
max=Number(document.getElementById('max').value),
speed=Number(document.getElementById('speed').value);
return (((min+max)/2)/speed);
}
</script>
<label for="min">Minimum Damage: </label>
<input type="text" id="min">
<label for="max">Maximum Damage: </label>
<input type="text" id="max">
<label for="speed">Speed: </label>
<input type="text" id="speed">

• ## Users who have thanked rangana for this post:

nathan130 (10-25-2008)

• Thanks alot, thats helping but theres still soem problems with it...

>It doesnt even calculate anything if you dont enter the speed first
If you enter stuff in a different order the answer changes so im guessing if you could fix this then it would work.

I tried Min Dam: 97 Max: 105 Speed: 2 Answer should be: 50.5 but it doesnt get the right answer and tried alot of things to see if you could enter half the speed or something to get the correct answer but nothing works so i hope you could fix it

• This modifies the code to add a button to compute. It seems more reliable so you can fill in all of the numbers and then it will run.

Code:
```<script type="text/javascript">
{
document.getElementById('run').onclick=function(){
document.getElementById('ans').value=compute();
}
function compute()
{
var min=Number(document.getElementById('min').value),
max=Number(document.getElementById('max').value),
speed=Number(document.getElementById('speed').value);
return (((min+max)/2)/speed);
}
</script>
<label for="min">Minimum Damage: </label>
<input type="text" id="min">
<label for="max">Maximum Damage: </label>
<input type="text" id="max">
<label for="speed">Speed: </label>
<input type="text" id="speed">
<input type="button" id="run" value="Calculate" />```
Its not nearly as slick, but I'm no JS programmer.

• That code doesnt input the answer.

• He just had a slight typo. He missed highlighted:
Code:
```window.onload=function()
{
document.getElementById('run').onclick=function(){
document.getElementById('ans').value=compute();
`}`
}```
...anyway, try this:
Code:
```<script type="text/javascript">
var ray={
compute:function(id)
{
var eq=((this.toNumber('min')+this.toNumber('max'))/2)/this.toNumber('speed');
if(isNaN(eq))
{
ray.show('ans','Error!');
return false;
}
else ray.show('ans',eq);
},
toNumber:function(el)
{return Number(document.getElementById(el).value);},
show:function(el,val)
{document.getElementById(el).value=val;}
};
</script>
<label for="min">Minimum Damage: </label>
<input type="text" id="min">
<label for="max">Maximum Damage: </label>
<input type="text" id="max">
<label for="speed">Speed: </label>
<input type="text" id="speed">
<input type="button" value="Calculate" onclick="ray.compute('ans')"/>```

• ## Users who have thanked rangana for this post:

nathan130 (10-27-2008)

• thank you guys so much, that works perfectly, im just going to resize the boxes and stuff and it will be exactly what i wanted!

•

#### Posting Permissions

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