0

This script contains logical error, where? It does only calculate avg. for first number, not second and so on...

window.onload = function() {
    var amountOfNumbers = 0;
    var total = 0;
    document.getElementById("uitkomst").innerHTML = "Er zijn nog geen cijfers ingevoerd";
    document.getElementById("cijfer").onblur = function() { 
        total = parseFloat(this.value);
        amountOfNumbers++;
        this.value = "";        
        document.getElementById("uitkomst").innerHTML = "Het gemiddelde van deze "+amountOfNumbers+" cijfers is "+(total/amountOfNumbers);
    }
};

1 Answer 1

3

You need to increment total rather than reset it.

total += parseFloat(this.value);
//    ^--- Add to the total

Without the + there, total will be equal to the current value, not the sum of all of the numbers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.