...

View Full Version : Calculation



nathan130
10-24-2008, 07:26 AM
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

rangana
10-24-2008, 08:09 AM
<script type="text/javascript">
window.onload=function()
{
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">
<label for="ans">Answer: </label>
<input type="text" id="ans" readonly="readonly">

nathan130
10-25-2008, 10:36 PM
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 :)

gnomeontherun
10-26-2008, 12:26 PM
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.


<script type="text/javascript">
window.onload=function()
{
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">
<label for="ans">Answer: </label>
<input type="text" id="ans" readonly="readonly">
<input type="button" id="run" value="Calculate" />

Its not nearly as slick, but I'm no JS programmer.

nathan130
10-27-2008, 05:11 AM
That code doesnt input the answer.

rangana
10-27-2008, 05:27 AM
He just had a slight typo. He missed highlighted:


window.onload=function()
{
document.getElementById('run').onclick=function(){
document.getElementById('ans').value=compute();
}
}


...anyway, try this:


<script type="text/javascript">
var ray={
compute:function(id)
{
var eq=((this.toNumber('min')+this.toNumber('max'))/2)/this.toNumber('speed');
if(isNaN(eq))
{
alert('Please rectify your inputs.');
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">
<label for="ans">Answer: </label>
<input type="text" id="ans" readonly="readonly">
<input type="button" value="Calculate" onclick="ray.compute('ans')"/>

nathan130
10-27-2008, 05:53 AM
:D 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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum