Skip to content

Instantly share code, notes, and snippets.

@peterschussheim
Last active September 14, 2016 17:36
Show Gist options
  • Save peterschussheim/eefd6a125713d1488f1f117bfa425223 to your computer and use it in GitHub Desktop.
Save peterschussheim/eefd6a125713d1488f1f117bfa425223 to your computer and use it in GitHub Desktop.
range.js
// 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