View Single Post
Old 02-14-2013, 08:04 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You could simply your code and trap NaN entries with

Code:
var n1 = Math.floor(Number(form.numb1.value))  || 0;  // i.e. assign 0 if the entry is NaN
DO NOT use the same name/id for an HTML elemnt and a Javascript variable.

parseInt() is really intended for converting from one number base to another. If you do use it you need to specify the radix (10) as otherwise if the user enters (e.g.) 09 it will be interpreted as octal. The correct way to obtain an integer is to use Math.floor(). Another example of poor quality teaching, but if parseInt() is required then I realise you must comply.

Instead of the fixed divisor /5 you need to keep count of the number of numbers entered. Without using an array, you could do that by assigning an initial value of "x" to n1, n2 etc. and then including in the calculation only those variables which are != x.

As Old Pedant says, onkeyup is totally inappropriate. You should use a button to trigger the calculation. I don't see how onchange is relevant either.

To centre (or center in American spelling ) your input boxes:-

Code:
<div id = "container" style = "text-align:center"} >
First Number: <input type="text" name="numb1" ><br>
Second Number: <input type="text" name = "numb2" ><br>
Third Number:  <input type="text" name="numb3"><br>
Fourth Number: <input type="text" name="numb4"><br>
Fifth Number:  <input type="text" name="numb5" ><br>
</div>
“Sex is one of the most wholesome, beautiful and natural experiences that money can buy.” - Steve Martin
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-14-2013 at 08:32 AM..
Philip M is online now   Reply With Quote