Tuesday, 27 August 2013

Get numerical values from input fields and use them to calculate the surface area of a trapezoid

Get numerical values from input fields and use them to calculate the
surface area of a trapezoid

I am new to Javascript and I will be thankful if someone help me
understand what I am doing wrong. I am trying to calculate the surface
area of a trapezoid by obtaining values from input fields. When I press
the "Calculate S" button I get a "Nan" as an answer. This is the major
part of the HTML code:
<form method="post">
<label for="a">a</label>
<input type="text" id="a"/> <br/>
<label for="b">b</label>
<input type="text" id="b"/> <br/>
<label for="h">h</label>
<input type="text" id="h"/> <br/>
<button onclick="alert(S)">Calculate S</button>
</form>
And this is the script I am using to obtain the values and calculate the
surface:
<script type="text/javascript">
var a=parseInt(document.getElementById("a"), 10);
var b=parseInt(document.getElementById("b"), 10);
var h=parseInt(document.getElementById("h"), 10);
var S=parseInt(((a+b)/2)*h, 10);
</script>
Thanks in advance!

No comments:

Post a Comment