2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: a3566b1109230028080c9345
|
|
|
|
|
challengeType: 5
|
2020-09-07 16:10:29 +08:00
|
|
|
|
forumTopicId: 16083
|
2020-10-01 17:54:21 +02:00
|
|
|
|
title: 范围内的数字求和
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-09-07 16:10:29 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
给出一个含有两个数字的数组,我们需要写一个函数,让它返回这两个数字间所有数字(包含这两个数字)的总和。
|
|
|
|
|
|
|
|
|
|
例如,<code>sumAll([4,1])</code> 应该返回 <code>10</code>,因为从 1 到 4 (包含 1、4)的所有数字的和是 <code>10</code>。
|
|
|
|
|
|
|
|
|
|
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>。
|
|
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-09-07 16:10:29 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-09-07 16:10:29 +08:00
|
|
|
|
- text: <code>sumAll([1, 4])</code>应该返回一个数字。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof sumAll([1, 4]) === 'number');
|
2020-09-07 16:10:29 +08:00
|
|
|
|
- text: <code>sumAll([1, 4])</code>应该返回 10。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert.deepEqual(sumAll([1, 4]), 10);
|
2020-09-07 16:10:29 +08:00
|
|
|
|
- text: <code>sumAll([4, 1])</code>应该返回 10。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert.deepEqual(sumAll([4, 1]), 10);
|
2020-09-07 16:10:29 +08:00
|
|
|
|
- text: <code>sumAll([5, 10])</code>应该返回 45。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert.deepEqual(sumAll([5, 10]), 45);
|
2020-09-07 16:10:29 +08:00
|
|
|
|
- text: <code>sumAll([10, 5])</code>应该返回 45。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert.deepEqual(sumAll([10, 5]), 45);
|
2018-10-10 18:03:03 -04: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'>
|
|
|
|
|
|
2020-09-07 16:10:29 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-09-07 16:10:29 +08:00
|
|
|
|
function sumAll(arr) {
|
|
|
|
|
var sum = 0;
|
|
|
|
|
arr.sort(function(a,b) {return a-b;});
|
|
|
|
|
for (var i = arr[0]; i <= arr[1]; i++) {
|
|
|
|
|
sum += i;
|
|
|
|
|
}
|
|
|
|
|
return sum;
|
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-09-07 16:10:29 +08:00
|
|
|
|
</section>
|