2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: ae9defd7acaf69703ab432ea
|
|
|
|
challengeType: 5
|
2020-09-07 16:10:29 +08:00
|
|
|
forumTopicId: 16075
|
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>arr</code>,它的返回值为这两个数字范围内所有数字(包含这两个数字)的最小公倍数。
|
|
|
|
注意,较小数不一定总是出现在数组的第一个元素。
|
|
|
|
比如,传入<code>[1, 3]</code>,那么函数的返回结果应为 1、2、3 的最小公倍数,即为 6。
|
|
|
|
如果你遇到了问题,请点击<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>smallestCommons([1, 5])</code>应该返回一个数字。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(typeof smallestCommons([1, 5]), 'number');
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>smallestCommons([1, 5])</code>应该返回 60。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(smallestCommons([1, 5]), 60);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>smallestCommons([5, 1])</code>应该返回 60。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(smallestCommons([5, 1]), 60);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>smallestCommons([2, 10])</code>应该返回 2520。.
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(smallestCommons([2, 10]), 2520);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>smallestCommons([1, 13])</code>应该返回 360360。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(smallestCommons([1, 13]), 360360);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>smallestCommons([23, 18])</code>应该返回 6056820。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(smallestCommons([23, 18]), 6056820);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function smallestCommons(arr) {
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
smallestCommons([1,5]);
|
|
|
|
```
|
|
|
|
|
|
|
|
</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 gcd(a, b) {
|
|
|
|
while (b !== 0) {
|
|
|
|
a = [b, b = a % b][0];
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
function lcm(a, b) {
|
|
|
|
return (a * b) / gcd(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
function smallestCommons(arr) {
|
|
|
|
arr.sort(function(a,b) {return a-b;});
|
|
|
|
var rng = [];
|
|
|
|
for (var i = arr[0]; i <= arr[1]; i++) {
|
|
|
|
rng.push(i);
|
|
|
|
}
|
|
|
|
return rng.reduce(lcm);
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2020-09-07 16:10:29 +08:00
|
|
|
</section>
|