Last active
November 24, 2020 15:02
-
-
Save benjamincharity/49fa0dbc1341db6ee7acf20378ec0722 to your computer and use it in GitHub Desktop.
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
function getSum(a: number, b: number): number { | |
if (a === b) { | |
return a; | |
} | |
const [start, end] = [a, b].sort((a, b) => a - b); | |
return Array(end - start + 1) | |
.fill(null) | |
.map((_, idx) => start + idx) | |
.reduce((acc, curr) => acc + curr); | |
} | |
const foo = getSum(-2, 7); | |
console.log(foo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment