Skip to content

Commit

Permalink
Use reduce for finding previous and next inputNums
Browse files Browse the repository at this point in the history
  • Loading branch information
j433866 committed Jul 3, 2019
1 parent 30a66f1 commit 367d30e
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/web/workers/InputWorker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,11 @@ self.getSmallestInputNum = function(inputNums) {
self.getPreviousInputNum = function(inputNum) {
const inputNums = Object.keys(self.inputs);
if (inputNums.length === 0) return -1;
let num = self.getSmallestInputNum(inputNums);
for (let i = 0; i < inputNums.length; i++) {
const iNum = parseInt(inputNums[i], 10);
if (iNum < inputNum) {
if (iNum > num) {
num = iNum;
}
}
}
return num;

return inputNums.reduce((acc, val) => {
val = parseInt(val, 10);
return (val < inputNum && val > acc) ? val : acc;
}, self.getSmallestInputNum(inputNums));
};

/**
Expand All @@ -368,16 +363,11 @@ self.getPreviousInputNum = function(inputNum) {
*/
self.getNextInputNum = function(inputNum) {
const inputNums = Object.keys(self.inputs);
let num = self.getLargestInputNum(inputNums);
for (let i = 0; i < inputNums.length; i++) {
const iNum = parseInt(inputNums[i], 10);
if (iNum > inputNum) {
if (iNum < num) {
num = iNum;
}
}
}
return num;

return inputNums.reduce((acc, val) => {
val = parseInt(val, 10);
return (val > inputNum && val < acc) ? val : acc;
}, self.getLargestInputNum(inputNums));
};

/**
Expand Down

0 comments on commit 367d30e

Please sign in to comment.