Skip to content

Instantly share code, notes, and snippets.

@hoyangtsai
Last active December 27, 2021 06:41
Show Gist options
  • Save hoyangtsai/c027418942ba823d2ced1d740ec5971c to your computer and use it in GitHub Desktop.
Save hoyangtsai/c027418942ba823d2ced1d740ec5971c to your computer and use it in GitHub Desktop.
javascript range array function #snippet #jsUtil
function range(start, edge, step) {
// If only one number was passed in make it the edge and 0 the start.
if (arguments.length == 1) {
edge = start;
start = 0;
}
// Validate the edge and step numbers.
edge = edge || 0;
step = step || 1;
// Create the array of numbers, stopping befor the edge.
for (var ret = []; (edge - start) * step > 0; start += step) {
ret.push(start);
}
return ret;
}
@hoyangtsai
Copy link
Author

A cool function that exists in Python is the range function which actually creates a list of numbers within the specified range.

From Chris West's blog his post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment