Last active
February 23, 2022 14:50
-
-
Save johnathanesanders/81d5a824da0c05c5e2b34ab8b4a86453 to your computer and use it in GitHub Desktop.
Generate an array of numbers, filled from start to length - allowing values to be skipped in a skipValues array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const generateArray = (start: number, length: number, skipValues: number[] = []): number[] => { | |
const outputArray: number[] = []; | |
// Length of array should be length + skipValues length since we will be skipping that many iterations of i | |
length = length + skipValues.length; | |
// Loop until proper array length is reached | |
for (let i=start;i<length-1;i++) { | |
// If the value of i isn't contained in skipValues, we push it to outputArray; | |
if (skipValues.indexOf(i) < 0) { | |
outputArray.push(i); | |
} else { | |
// Skip the value because it's contained in skipValues. Since we did this, | |
// and to ensure proper length of outputArray, we increment i and move on. | |
i++; | |
} | |
} | |
// Return the result | |
return outputArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment