Thread: Javascript homework / averaging calculator

1. Registered User
Join Date
Dec 2011
Posts
3

Javascript homework / averaging calculator

Sorry to bother with a newbie question, but here goes.

I am very new to Javascript. 2nd assignment using Javascript, final assignment of the year. I am calculating an average in a form for 3 bowling scores.

I have tagged the form for each score as game1, game2, and game 3. I am assigned to add a variable to each as gameOne, gameTwo, and gameThree. All data entered must be not less than zero or greater than 300, numeric only.

I will be using function avergCalc() to validate the data entered
I must also calculate average (with a different function?..not quite sure of how)
The average score will be avgGame, and must use the Math.round() method to round to whole number.

Any help is appreciated. I will be posting with the methods I have as of the moment.

Thanks in advance for any help or advice. I've been using jslint.com to validate.

2. Registered User
Join Date
Dec 2011
Posts
3
Still stuck, the closest I have gotten was NaN in my average value. I believe I am adding an extra step, but here goes.

function avergCalc() {
gameOne = parseInt(averageCalc.game1.value, 10);
if (isNaN(gameOne) || (gameOne <= 0) || (gameOne > 300)) {
alert("Entries must be less than 0 or greater than 300!");
averageCalc.game1.value = "";
averageCalc.game1.focus();
} else {
gameTwo = parseInt(averageCalc.game2.value, 10);
if (isNaN(gameTwo) || (gameTwo <= 0) || (gameTwo > 300)) {
alert("Entries must be less than 0 or greater than 300!");
averageCalc.game2.value = "";
averageCalc.game2.focus();
} else {
var gameThree = parseInt(averageCalc.game3.value, 10);
if (isNaN(gameThree) || (gameThree <= 0) || (gameThree > 300)) {
alert("Entries must be less than 0 or greater than 300!");
averageCalc.game3.value = "";
averageCalc.game3.focus();
} else {
var avgGame = averAge(gameOne, gameTwo, gameThree);
averageCalc.Avg.value = (avgGame.toString());
}
}
}
}

function averAge(gameOne, gameTwo, gameThree) {
var bowlAvg = (gameOne + gameTwo + gameThree) / 3;
return bowlAvg
}

3. Registered User
Join Date
Dec 2011
Posts
3