문제
We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them.
The lowest number will not always come first.
- 예시
sumAll([1, 4]) // should return 10. sumAll([10, 5]) // should return 45.
내가 푼 답
function sumAll(arr) { var sorted = [].concat(arr).sort((a,b) => a-b); var result = 0; for(var i = sorted[0] ; i <= sorted[1] ; i++ ) { result += i; } return result; }
Advanced Solution
function sumAll(arr) { var sum = 0; for (var i = Math.min(...arr); i <= Math.max(...arr); i++){ sum += i; } return sum; }