0

My code works but my math is off! How can I get my code to find the sum of odd and even numbers?

function getNum()
{
    var div = document.getElementById("output");
    div.innerHTML = "Numbers entered:<br>";

    var score = 0; 
    var sum = 0;
    var count = 0; 
    var average = 0;
    var oddCount = 0;
    var evenCount = 0;
    var label = "";
    var odd = 0;
    var even = 0;

    while (score != -999)
    {
        sum = sum + score;
        count = count + 1;   //or count++
        score = parseInt(prompt("Enter an integer (or enter -999 to quit:"," "),10);
        if(score % 2)
        {
            label = " is odd";
            //even = even + score;
        }
        else
        {
            label = " is even";
            //odd = odd + score;
        }

        div.innerHTML = div.innerHTML + score + label + "<br>";

        //find odd score and even score
        if ((score != -999) && ((score % 2) === 0)) 
        {
            evenCount++;
        }
        if ((score != -999) && ((score % 2) !== 0))
        {
            oddCount++;
        }
    }
    count = count - 1; //dont count -999
    sum = sum + oddCount;
    average = sum/oddCount;
    div.innerHTML = div.innerHTML + "You entered: " + oddCount + " odd numbers." + "<br>";
    div.innerHTML = div.innerHTML + "The sum of these numbers is " + sum + ".<br>";
    div.innerHTML = div.innerHTML + "The average of these numbers = " + sum + " / " + oddCount + " = " + average.toFixed(2) + ".<br><br>";
    count = count - 1; //dont count -999
    sum = sum + evenCount;
    average = sum/evenCount;
    div.innerHTML = div.innerHTML + "You entered: " + evenCount + " even numbers." + "<br>";
    div.innerHTML = div.innerHTML + "The sum of these numbers is " + sum + ".<br>";
    div.innerHTML = div.innerHTML + "The average of these numbers = " + sum + " / " + evenCount + " = " + average.toFixed(2) + ".<br>";

}// end of function
1
Contributor
1
Reply
5
Views
3 Years
Discussion Span
Last Post by lordofkingz
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.