Skip to content

Instantly share code, notes, and snippets.

@johnathanesanders
Last active February 23, 2022 14:50
Show Gist options
  • Save johnathanesanders/81d5a824da0c05c5e2b34ab8b4a86453 to your computer and use it in GitHub Desktop.
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
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