www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript homework / averaging calculator

  1. #1
    Join Date
    Dec 2011
    Posts
    3

    Question 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. #2
    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. #3
    Join Date
    Dec 2011
    Posts
    3
    i got it, finally. please delete thread. thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles