commit
message
Formulas.AVERAGEA written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-22 19:34:10
stats
2 file(s) changed,
71 insertions(+),
2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
2index 385da38..74cadf6 100644
3--- a/src/RawFormulas/RawFormulas.ts
4+++ b/src/RawFormulas/RawFormulas.ts
5@@ -40,6 +40,29 @@ function flatten(values: Array<any>) : Array<any> {
6 }, []);
7 }
8
9+/**
10+ * Converts string values in array to 0
11+ * @param arr to convert
12+ * @returns {Array} array in which all string values have been converted to 0.
13+ */
14+function stringValuesToZeros(arr: Array<any>) : Array<any> {
15+ var toReturn = [];
16+ for (var i = 0; i < arr.length; i++) {
17+ if (typeof arr[i] !== "string") {
18+ toReturn.push(arr[i]);
19+ } else {
20+ toReturn.push(0);
21+ }
22+ }
23+ return toReturn;
24+}
25+
26+/**
27+ * Calculates the average of the magnitudes of deviations of data from a dataset's mean.
28+ * @param values The value(s) or range(s)
29+ * @returns {number} average of the magnitudes of deviations of data from a dataset's mean
30+ * @constructor
31+ */
32 var AVEDEV = function (...values) {
33 checkArgumentsAtLeastLength(values, 1);
34
35@@ -63,7 +86,6 @@ var AVEDEV = function (...values) {
36 return valueToNumber(value);
37 }).concat(nonArrayValues);
38
39-
40 // Calculating mean
41 var result = 0;
42 var count = 0;
43@@ -79,9 +101,33 @@ var AVEDEV = function (...values) {
44 return SUM(flatValues) / flatValues.length;
45 };
46
47+/**
48+ * Returns the numerical average value in a dataset, coercing text values in ranges to 0 values.
49+ * @param values value(s) or range(s) to consider when calculating the average value.
50+ * @returns {number} the numerical average value in a dataset
51+ * @constructor
52+ */
53+var AVERAGEA = function (...values) {
54+ checkArgumentsAtLeastLength(values, 1);
55+ var result = 0;
56+ var count = 0;
57+ for (var i = 0; i < values.length; i++) {
58+ if (values[i] instanceof Array) {
59+ if (values[i].length === 0) {
60+ throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
61+ }
62+ var filtered = stringValuesToZeros(values[i]);
63+ result = result + SUM.apply(this, filtered);
64+ count += filtered.length;
65+ } else {
66+ result = result + valueToNumber(values[i]);
67+ count++;
68+ }
69+ }
70+ return result / count;
71+};
72
73 var ACCRINT = Formula["ACCRINT"];
74-var AVERAGEA = Formula["AVERAGEA"];
75 var AVERAGEIF = Formula["AVERAGEIF"];
76 var BASE = Formula["BASE"];
77 var BIN2DEC = Formula["BIN2DEC"];
78diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
79index 90ebb5c..b7b6a90 100644
80--- a/tests/FormulasTest.ts
81+++ b/tests/FormulasTest.ts
82@@ -230,7 +230,28 @@ catchAndAssertEquals(function() {
83 AVERAGE([]);
84 }, ERRORS.REF_ERROR);
85
86-assertEquals(AVERAGEA(10, 20, 4.1), 11.366666666666667);
87+
88+// Test AVERAGEA
89+assertEquals(AVERAGEA(1, 2, 4, 55), 15.5);
90+assertEquals(AVERAGEA(1, 2, 4, "55"), 15.5);
91+assertEquals(AVERAGEA(1, 2, 4, 55, false), 12.4);
92+assertEquals(AVERAGEA(1, 2, 4, 55, true), 12.6);
93+assertEquals(AVERAGEA(1, 2, 4, 55, 0), 12.4);
94+assertEquals(AVERAGEA(1, 2, 4, 55, 1), 12.6);
95+catchAndAssertEquals(function() {
96+ AVERAGEA(1, 2, 4, "str");
97+}, ERRORS.VALUE_ERROR);
98+assertEquals(AVERAGEA([1, 2, 4, 55, "str"]), 12.4);
99+assertEquals(AVERAGEA([1, 2, 4, 55, "22"]), 12.4);
100+assertEquals(AVERAGEA([1, 2, 4, 55, 0]), 12.4);
101+assertEquals(AVERAGEA([0]), 0);
102+catchAndAssertEquals(function() {
103+ AVERAGEA();
104+}, ERRORS.NA_ERROR);
105+catchAndAssertEquals(function() {
106+ AVERAGEA([]);
107+}, ERRORS.REF_ERROR);
108+
109
110 assertEquals(AVERAGEIF([1, 5, 10], '>2'), 7.5);
111