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
* @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) {
num = parseFloat(splitNumbers[i].trim());
} else {
num = parseInt(splitNumbers[i].trim(), 10);
num = parseInt(splitNumbers[i].trim(), 0);
}
if (num !== "NaN") {
numbers.append(num);
if (!isNaN(num)) {
numbers.push(num);
}
}
num = Arithmetic.opMap[args[1] || "Sum"](numbers);
if (num !== null) {
return "The values " + args[1] + "equal: " + num;
if (num === null) {
return "";
}
throw "Error with Arithmetic Operation: " + args[1];
return num;
},
@ -61,11 +69,11 @@ const Arithmetic = {
* @returns {number}
*/
_sum: function(data) {
let total = 0;
for (let i = 0; i < data.length; i++) {
total += data[i];
if (data.length > 0) {
return data.reduce((acc, curr) => acc + curr);
} else {
return null;
}
return total;
},
/**
@ -76,16 +84,11 @@ const Arithmetic = {
* @returns {number}
*/
_sub: function(data) {
let total = 0;
if (data.length > 1) {
total = data[0];
for (let i = 1; i < data.length; i++) {
total -= data[i];
}
if (data.length > 0) {
return data.reduce((acc, curr) => acc - curr);
} else {
total = null;
return null;
}
return total;
},
/**
@ -96,16 +99,11 @@ const Arithmetic = {
* @returns {number}
*/
_multiply: function(data) {
let total = 0;
if (data.length > 1) {
total = data[0];
for (let i = 1; i < data.length; i++) {
total *= data[i];
}
if (data.length > 0) {
return data.reduce((acc, curr) => acc * curr);
} else {
total = null;
return null;
}
return total;
},
/**
@ -116,16 +114,11 @@ const Arithmetic = {
* @returns {number}
*/
_divide: function(data) {
let total = 0;
if (data.length > 1) {
total = data[0];
for (let i = 1; i < data.length; i++) {
total /= data[i];
}
if (data.length > 0) {
return data.reduce((acc, curr) => acc / curr);
} else {
total = null;
return null;
}
return total;
},
/**
@ -136,27 +129,82 @@ const Arithmetic = {
* @returns {number}
*/
_mean: function(data) {
let total = 0;
if (data.length > 1) {
total = Arithmetic._sum(data) / data.length;
if (data.length > 0) {
return Arithmetic._sum(data) / data.length;
} 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.
*
* @constant
*/
opMap: {
"Sum": function(numArray) { return Arithmetic._sum(numArray); },
"Sub": function(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); },
"Sum": function(numArray) {
return Arithmetic._sum(numArray);
},
"Sub": function(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;