Skip to content

Instantly share code, notes, and snippets.

@oliverjam
Created November 8, 2016 15:02
Show Gist options
  • Save oliverjam/504ac967fb6815ba66af8c2d9ee3efb1 to your computer and use it in GitHub Desktop.
Save oliverjam/504ac967fb6815ba66af8c2d9ee3efb1 to your computer and use it in GitHub Desktop.
EJ: C4 P1 created by oliverjam - https://repl.it/ESBy/1
function range(start, end, step) {
let result = [];
if (step === undefined && start < end) {
step = 1
} else if (step === undefined && start > end) {
step = -1;
}
if (step < 0) {
for (let i = start; i>= end; i += step) {
result.push(i);
}
} else {
for (let i = start; i <= end; i += step) {
result.push(i);
}
}
return result;
}
function sum(arr) {
return arr.reduce((prev, curr) => prev + curr);
}
console.log(range(5, 2));
console.log(range(1, 10));
console.log(range(5, 2, -1));
console.log(range(1, 10, 2));
console.log(sum(range(1, 10)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment