Created
November 8, 2016 15:02
-
-
Save oliverjam/504ac967fb6815ba66af8c2d9ee3efb1 to your computer and use it in GitHub Desktop.
EJ: C4 P1 created by oliverjam - https://repl.it/ESBy/1
This file contains hidden or 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
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