All files index.js

100% Statements 37/37
93.75% Branches 15/16
100% Functions 16/16
100% Lines 37/37
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123              1x           2x             4x 2x     2x 40x     2x             2x             4x 2x     2x 40x     2x             2x             4x 2x     2x 40x     2x     2x     4x 2x     2x 38x     2x             2x 1x     1x 20x     1x             2x 1x     1x 20x     1x       1x 1x  
/**
 * @name array-means
 * @version 1.4.1
 * @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
     */
    a: function (arr) {
        return this.arithmetic(arr);
    },
    /**
     * @param {Array<Number>} arr - The array of which the arithmetic mean is calculated on
     * @returns {Number} The arithmetic mean
     */
    arithmetic: function (arr) {
        if (!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
     */
    q: function(arr) {
        return this.quadratic(arr);
    },
    /**
     * @param {Array<Number>} arr - The array of which the quadratic mean is calculated on
     * @returns {Number} The quadratic mean
     */
    quadratic: function(arr) {
        if (!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
     */
    h: function(arr) {
        return this.harmonic(arr);
    },
    /**
     * @param {Array<Number>} arr - The array of which the harmonic mean is calculated on
     * @returns {Number} The harmonic mean
     */
    harmonic: function(arr) {
        if (!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;
    },
    g: function(arr) {
        return this.geometric(arr);
    },
    geometric: function(arr) {
        if (!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);
    },
    /**
     * @param {Array<Number>} arr - The array of which the cubic mean is calculated on
     * @returns {Number} The cubic mean
     */
    c: function(arr) {
        if (!Array.isArray(arr)) {
            throw new Error("Argument is not an array!");
        }
 
        var result = arr.reduce(function(a, b) {
            return a + Math.pow(b, 3);
        }, 0);
 
        return Math.pow(result / arr.length, 1 / 3);
    },
    /**
     * @param {Array<Number>} arr - The array of which the cubic mean is calculated on
     * @returns {Number} The cubic mean
     */
    cubic: function(arr) {
        if (!Array.isArray(arr)) {
            throw new Error("Argument is not an array!");
        }
 
        var result = arr.reduce(function(a, b) {
            return a + Math.pow(b, 3);
        }, 0);
 
        return Math.pow(result / arr.length, 1 / 3);
    }
};
 
Eif (typeof module !== "undefined" && module.exports) {
    module.exports = arrayMeans;
}