Skip to content

Instantly share code, notes, and snippets.

@xarxziux
Last active May 2, 2017 23:44
Show Gist options
  • Save xarxziux/3af708218d653f6ee64b458191470696 to your computer and use it in GitHub Desktop.
Save xarxziux/3af708218d653f6ee64b458191470696 to your computer and use it in GitHub Desktop.
Demonstratory JavaScript module for testing for the presence of elements within given ranges in a numeric array.
/*
* The requirement for this module is to accept a number array
* and test for the presence of at least one element in each of
* the following ranges:
*
* 10-20
* 20-30
* 30-40
* 40-50
* 50-60
* 60-70
* 70-80
* 80-90
*
* Each range includes the first number but not the second.
*
* The first step is to start with an array of falses. Each index
* is this array corresponds to one of the ranges above. False means
* that no example has been found. True means that at least one has.
*
* The initial value is all false since no items have been processed yet.
*/
const rangeArr = [false,false,false,false,false,false,false,false];
/*
* Now we need a helper function to process one number at a time. This
* function accepts an array of booleans and number. It then tests if
* that value is within one of the ranges listed above, and updates the
* appropriate index in the boolean array.
*
* Note that this function copies the original input array to avoid mutating
* it. This will be very inefficient for larger arrays.
*/
function checkItem (arr, x) {
const arr1 = arr.slice();
let xIndex;
if (x < 10) xIndex = -1;
else if (x < 20) xIndex = 0;
else if (x < 30) xIndex = 1;
else if (x < 40) xIndex = 2;
else if (x < 50) xIndex = 3;
else if (x < 60) xIndex = 4;
else if (x < 70) xIndex = 5;
else if (x < 80) xIndex = 6;
else if (x < 90) xIndex = 7;
else xIndex = -1;
if ((xIndex !== -1) && (!arr[xIndex]))
arr1[xIndex] = true;
return arr1;
}
/*
* This function updates the input array given two conditions:
*
* The given number is within the range 10 - 90 and,
* The index for that range is false.
*
* It then returns the new, updated array.
*
* To use this function, we can now use it as the first parameter to the
* Array.prototype.reduce function. This function applies the supplied
* function to each item of the array in turn, and passes the return
* value through to the next iteration. We also provide the array of
* falses to the function as the initial value.
*/
function checkRanges (arr) {
return arr.reduce (checkItem, rangeArr);
}
/*
* The return value from this function will be an array of eight
* booleans which will communicate whether an example of each range
* has been encountered. To find out if all items in this array are true,
* we can use another helper function and reduce that.
*/
function bothTrue (x, y) {
return x && y;
}
/*
* This function returns false if either parameter is false. By reducing
* over this, we can determine if any one of the elements in the boolean
* array is false.
*/
function isAllTrue (arr) {
return arr.reduce (bothTrue);
}
/*
* Note that we don't necessarily need to use that function, if the
* intermediate result is not required. Instead, we can simply reduce
* over both helper functions to return a single boolean.
*
* Putting it all together, the next function first reduces over
* the number array to get an array of booleans, then reduces over that to
* get a single boolean value.
*/
function checkAll (arr) {
return arr.reduce (checkItem, rangeArr)
.reduce (bothTrue);
}
/*
* Now we just need to export the functions. For the purposes of this
* demo we'll export everything, but this isn't necessarily how you would
* want to it in production code.
*
* Of course, this is making the assumption that you're using Node. If not,
* you'll need to adapt this to your own use case.
*
* Please note that this algorithm is not the fastest or most efficient.
* One particular ineffifiency is that if the boolean array all true, the
* reduce function will still run through to the end of the array, but it
* should be logical enough to understand. You will still need to do quite
* a bit more work to make this into production-ready code.
*/
module.exports = {
rangeArr,
checkItem,
checkRanges,
bothTrue,
isAllTrue,
checkAll
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment