This Gist was automatically created by Carbide, a free online programming environment.
Last active
September 14, 2016 17:36
-
-
Save peterschussheim/eefd6a125713d1488f1f117bfa425223 to your computer and use it in GitHub Desktop.
range.js
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
// range :: (Number, Number, Number) -> [Number] | |
function range(start, end, step) { | |
var results = []; | |
if (step == null) { ///If \`step\` parameter is not provided (null), set \`step\` to default to 1. | |
step = 1; | |
} | |
if (step > 0) { ///If \`step\` is a \*\*positive\*\* value:\\- iterate beginning at the \`start\` value//- continue while \`i\` <= end param//- increment \`i\` using \`step\` value//- push \`i\` to results array. | |
for (var i = start; i <= end; i += step) { | |
results.push(i); | |
} | |
} else { | |
for (var i = start; i >= end; i += step) { ///Else, we assume \`step\` is a **negative** value:\\- iterate beginning at the \`start\` value//- continue while **\`i\` >= end** param//- **decrement** \`i\` using \`step\` value//- push \`i\` to results array. | |
results.push(i); | |
} | |
} | |
return results; ///Return the results array. | |
} | |
range(100, 50, -3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment