All files index.js

82.61% Statements 19/23
58.33% Branches 7/12
100% Functions 8/8
82.61% Lines 19/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69              1x           1x       1x 20x     1x             1x       1x 20x     1x             1x       1x 20x     1x     1x       1x 19x     1x       1x 1x  
/**
 * @name array-means
 * @version 1.4.0
 * @description Calculates various averages of an array
 * @author André Lichtenthäler
 * @license GPL-3.0
 */
var arrayMeans = {
    /**
     * @param {Array<Number>} arr - The array of which the arithmetic mean is calculated on
     * @returns {Number} The arithmetic mean
     */
    arithmetic: function (arr) {
        Iif (!Array.isArray(arr)) {
            throw new Error("Argument is not an array!");
        }
 
        var result = arr.reduce(function(a, b) {
            return a + b;
        }, 0);
 
        return result / arr.length;
    },
    /**
     * @param {Array<Number>} arr - The array of which the quadratic mean is calculated on
     * @returns {Number} The quadratic mean
     */
    quadratic: function(arr) {
        Iif (!Array.isArray(arr)) {
            throw new Error("Argument is not an array!");
        }
 
        var result = arr.reduce(function(a, b) {
            return a + Math.pow(b, 2);
        }, 0);
 
        return Math.sqrt(result / arr.length);
    },
    /**
     * @param {Array<Number>} arr - The array of which the harmonic mean is calculated on
     * @returns {Number} The harmonic mean
     */
    harmonic: function(arr) {
        Iif (!Array.isArray(arr)) {
            throw new Error("Argument is not an array!");
        }
 
        var result = arr.reduce(function(a, b) {
            return a + (1 / b);
        }, 0);
 
        return arr.length / result;
    },
    geometric: function(arr) {
        Iif (!Array.isArray(arr)) {
            throw new Error("Argument is not an array!");
        }
 
        var result = arr.reduce(function(a, b) {
            return a *= b;
        });
 
        return Math.pow(result, 1 / arr.length);
    }
};
 
Eif (typeof module !== "undefined" && module.exports) {
    module.exports = arrayMeans;
}