Simple average function in Javascript

Use the array’s length property:

function calculate(array) {
    var i = 0, sum = 0, len = array.length;
    while (i < len) {
        sum = sum + array[i++];
    }
    return sum / len;
}

Leave a Comment

tech