2018-09-30 23:01:58 +01:00
---
id: a3566b1109230028080c9345
title: Sum All Numbers in a Range
isRequired: true
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16083
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2019-03-02 03:26:23 -04:00
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.
For example, < code > sumAll([4,1])< / code > should return < code > 10< / code > because sum of all the numbers between 1 and 4 (both inclusive) is < code > 10< / code > .
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +03:00
- text: < code > sumAll([1, 4])</ code > should return a number.
2019-07-24 01:56:38 -07:00
testString: assert(typeof sumAll([1, 4]) === 'number');
2018-10-20 21:02:47 +03:00
- text: < code > sumAll([1, 4])</ code > should return 10.
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(sumAll([1, 4]), 10);
2018-10-20 21:02:47 +03:00
- text: < code > sumAll([4, 1])</ code > should return 10.
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(sumAll([4, 1]), 10);
2018-10-20 21:02:47 +03:00
- text: < code > sumAll([5, 10])</ code > should return 45.
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(sumAll([5, 10]), 45);
2018-10-20 21:02:47 +03:00
- text: < code > sumAll([10, 5])</ code > should return 45.
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(sumAll([10, 5]), 45);
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function sumAll(arr) {
return 1;
}
sumAll([1, 4]);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function sumAll(arr) {
var sum = 0;
arr.sort(function(a,b) {return a-b;});
for (var i = arr[0]; i < = arr[1]; i++) {
2018-10-08 01:01:53 +01:00
sum += i;
2018-09-30 23:01:58 +01:00
}
return sum;
}
```
< / section >