1
0
mirror of synced 2024-11-17 11:37:13 +01:00

Inital commit for pull

This commit is contained in:
bwhitn 2017-12-17 21:57:09 -05:00
parent ef0d3b73b0
commit f9ddee7d80

View File

@ -21,7 +21,15 @@ const Arithmetic = {
* @constant * @constant
* @default * @default
*/ */
OPERATIONS: ["Sum", "Sub", "Multiply", "Divide", "Mean", "Median", "Mode"], OPERATIONS: [
"Sum",
"Sub",
"Multiply",
"Divide",
"Mean",
"Median",
"Standard Deviation"
],
/** /**
* *
@ -39,17 +47,17 @@ const Arithmetic = {
if (splitNumbers[i].indexOf(".") >= 0) { if (splitNumbers[i].indexOf(".") >= 0) {
num = parseFloat(splitNumbers[i].trim()); num = parseFloat(splitNumbers[i].trim());
} else { } else {
num = parseInt(splitNumbers[i].trim(), 10); num = parseInt(splitNumbers[i].trim(), 0);
} }
if (num !== "NaN") { if (!isNaN(num)) {
numbers.append(num); numbers.push(num);
} }
} }
num = Arithmetic.opMap[args[1] || "Sum"](numbers); num = Arithmetic.opMap[args[1] || "Sum"](numbers);
if (num !== null) { if (num === null) {
return "The values " + args[1] + "equal: " + num; return "";
} }
throw "Error with Arithmetic Operation: " + args[1]; return num;
}, },
@ -61,11 +69,11 @@ const Arithmetic = {
* @returns {number} * @returns {number}
*/ */
_sum: function(data) { _sum: function(data) {
let total = 0; if (data.length > 0) {
for (let i = 0; i < data.length; i++) { return data.reduce((acc, curr) => acc + curr);
total += data[i]; } else {
return null;
} }
return total;
}, },
/** /**
@ -76,16 +84,11 @@ const Arithmetic = {
* @returns {number} * @returns {number}
*/ */
_sub: function(data) { _sub: function(data) {
let total = 0; if (data.length > 0) {
if (data.length > 1) { return data.reduce((acc, curr) => acc - curr);
total = data[0];
for (let i = 1; i < data.length; i++) {
total -= data[i];
}
} else { } else {
total = null; return null;
} }
return total;
}, },
/** /**
@ -96,16 +99,11 @@ const Arithmetic = {
* @returns {number} * @returns {number}
*/ */
_multiply: function(data) { _multiply: function(data) {
let total = 0; if (data.length > 0) {
if (data.length > 1) { return data.reduce((acc, curr) => acc * curr);
total = data[0];
for (let i = 1; i < data.length; i++) {
total *= data[i];
}
} else { } else {
total = null; return null;
} }
return total;
}, },
/** /**
@ -116,16 +114,11 @@ const Arithmetic = {
* @returns {number} * @returns {number}
*/ */
_divide: function(data) { _divide: function(data) {
let total = 0; if (data.length > 0) {
if (data.length > 1) { return data.reduce((acc, curr) => acc / curr);
total = data[0];
for (let i = 1; i < data.length; i++) {
total /= data[i];
}
} else { } else {
total = null; return null;
} }
return total;
}, },
/** /**
@ -136,27 +129,82 @@ const Arithmetic = {
* @returns {number} * @returns {number}
*/ */
_mean: function(data) { _mean: function(data) {
let total = 0; if (data.length > 0) {
if (data.length > 1) { return Arithmetic._sum(data) / data.length;
total = Arithmetic._sum(data) / data.length;
} else { } else {
total = null; return null;
}
},
/**
* Finds the median of a number array and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_median: function (data) {
if ((data.length % 2) === 0) {
let first, second;
data.sort(function(a, b){
return a - b;
});
first = data[Math.floor(data.length / 2)];
second = data[Math.floor(data.length / 2) - 1];
return Arithmetic._mean([first, second]);
} else {
return data[Math.floor(data.length / 2)];
}
},
/**
* Finds the standard deviation of a number array and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_stdDev: function (data) {
if (data.length > 0) {
let avg = Arithmetic._mean(data);
let devSum = 0;
for (let i = 0; i < data.length; i++) {
devSum += (data[i] - avg) ** 2;
}
return Math.sqrt(devSum / data.length);
} else {
return null;
} }
return total;
}, },
/** /**
* A mapping of operation names to their function. * A mapping of operation names to their function.
*
* @constant * @constant
*/ */
opMap: { opMap: {
"Sum": function(numArray) { return Arithmetic._sum(numArray); }, "Sum": function(numArray) {
"Sub": function(numArray) { return Arithmetic._sub(numArray); }, return Arithmetic._sum(numArray);
"Multiply": function(numArray) { return Arithmetic._multiply(numArray); }, },
"Divide": function(numArray) { return Arithmetic._divide(numArray); }, "Sub": function(numArray) {
"Mean": function(numArray) { return Arithmetic._mean(numArray); }, return Arithmetic._sub(numArray);
},
"Multiply": function(numArray) {
return Arithmetic._multiply(numArray);
},
"Divide": function(numArray) {
return Arithmetic._divide(numArray);
},
"Mean": function(numArray) {
return Arithmetic._mean(numArray);
},
"Median": function(numArray) {
return Arithmetic._median(numArray);
},
"Standard Deviation": function (numArray) {
return Arithmetic._stdDev(numArray);
},
}, },
}; };
export default Arithmetic; export default Arithmetic;