# Calculations!

• 06-26-2013, 02:28 PM
shaaz
Calculations!
Afternoon :)

I have just started learning JavaScript and I'm trying to create a calculator that subtracts two values in an input field and then divides it by 4. I would like the result to be displayed on another field whenever the user clicks in "Calculate". However, whenever I try clicking in Calculate nothing happens.

This is my code:

Code:

```                <form name "calc" >                 Life expectancy       <input name="average" value="0" maxlength="3">               <p> Age       <input name="age" value="0" maxlength="3">       </p>             <p>       <input type="button" value="Calculate" onClick="calculation()" />       </p>       <p>       Result       <input type="text" name="result" size="10" disabled/>       </p>             </form>```
Would really appreciate if someone could help me out! :D
Thank you!
• 06-26-2013, 03:00 PM
shaaz
Oh and the function I have used is

Code:

```<script>                 function calculation (){             var age, average, total;     average = parseInt(document.calc.getElementById("average").value, 10);     age = parseInt(document.calc.getElementById("age").value, 10);     total = average - age;     document.calc.getElementById("result") = total / 4;     }                     </script>```
Thank you again :)
• 06-26-2013, 03:01 PM
vwphillips
Code:

```<!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" xml:lang="en" lang="en"> <head>   <title></title> <script type="text/javascript"> /*<![CDATA[*/ function calculation(button,le,ag,re,by){  var frm=button.form;  if (frm){   var le=frm[le],ag=frm[ag],re=frm[re]; // convert the strings to form elements   if (le&&ag&&re){   le=(le.value).replace(/\D/g,''); // remove none numbers   le.value=le||0;   ag=(ag.value).replace(/\D/g,'');   ag.value=le||0;   re.value=(le-ag)*(by||1);  // le and ag are strings but subtraction or multiplication converts them to numbers   }  } } /*]]>*/ </script></head> <body>         <form name="calc" >         Life expectancy       <input name="average" value="0" maxlength="3">       <p> Age       <input name="age" value="0" maxlength="3">       </p>       <p>       <input type="button" value="Calculate" onClick="calculation(this,'average','age','result',4)" />       </p>       <p>       Result       <input type="text" name="result" size="10" disabled/>       </p>       </form> </body> </html>```
• 06-26-2013, 03:16 PM
VIPStephan
The name attribute is deprecated (except on form controls) and should therefore not be used on the form element. Use `id` instead and address the form with `document.getElementById('calc')`. Also, use labels with your inputs.
• 06-26-2013, 07:22 PM
Old Pedant
In other words, if you change to
Code:

`<form id="calc">`
then your JS code could be:
Code:

```<script type="text/javascript">             function calculation () {     var form, age, average, total;     form = document.getElementById("calc");            average = parseInt( form.average.value, 10);     age = parseInt(form.age.value,10);     total = average - age;     form.result.value = total / 4; }                 </script>```
• 06-26-2013, 09:26 PM
felgall
You can leave out the two lines calling parseInt to convert the numbers - the subtraction on the following line will do that anyway.
• 06-26-2013, 10:06 PM
Old Pedant
Well, you can't leave out the lines. You can just leave out the parseInt() call.

Or you could leave out the lines by some minor rewriting:
Code:

```<script type="text/javascript">             function calculation () {     var form = document.getElementById("calc");            form.result.value = ( form.average.value - form.age.value ) / 4; }                 </script>```