![]() |
Need help with JS average calculator
I need some help getting this to work. I cant get the average function to work. I have tried F12 console no errors. Where am I going wrong?
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" this is what i get text (box) text (box) text (box) what i want is text(box) text(box) text(box) centered horizonally on the web page *(box)= textbox |
What *POSSIBLE* reason is there to use onkeyup???
The average is *MEANINGLESS* until all 5 numbers have been entered. You are the second person today using onkeyup for no discernible reason. Where are you learning that from? |
LOL! This is *FUNNY*!
Code:
var average = document.getElementById('average'); |
Code:
<!doctype html> |
Yeah, MrHoo, but what if the user has only entered 3 values? With your code, the blank values will be treated as zeroes and the average for 3 values won't be accurate.
|
Edit
I have to use:
onchange event handler calcAvg() performCalc() calcResult averageCalc() parseInt() Which sucks because I got it the way I like and now I got to change it |
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 NaNparseInt() 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"} > |
| All times are GMT +1. The time now is 05:00 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.