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

Thread: Calculation

  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    61
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Question 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
    Last edited by nathan130; 10-24-2008 at 06:30 AM.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Code:
    <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">
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    nathan130 (10-25-2008)

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    61
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    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">
    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.
    jeremy - gnomeontherun
    Educated questions often get educated answers, and simple questions often get simple answers.

  • #5
    New Coder
    Join Date
    Jul 2008
    Posts
    61
    Thanks
    10
    Thanked 0 Times in 0 Posts
    That code doesnt input the answer.

  • #6
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    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))
    		{
    		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')"/>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    nathan130 (10-27-2008)

  • #7
    New Coder
    Join Date
    Jul 2008
    Posts
    61
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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
    •