Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
arguments-optional.english.md
binary-agents.english.md
convert-html-entities.english.md
diff-two-arrays.english.md
dna-pairing.english.md
drop-it.english.md
everything-be-true.english.md
make-a-person.english.md
map-the-debris.english.md
missing-letters.english.md
pig-latin.english.md
search-and-replace.english.md
seek-and-destroy.english.md
smallest-common-multiple.english.md
sorted-union.english.md
spinal-tap-case.english.md
steamroller.english.md
sum-all-numbers-in-a-range.english.md
sum-all-odd-fibonacci-numbers.english.md
sum-all-primes.english.md
wherefore-art-thou.english.md
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
formattingConversion
math-challenges
requiresTests
schema
test
.babelrc
.editorconfig
.eslintignore
.eslintrc
.npmignore
.prettierrc
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
repack.js
unpack.js
unpacked.css
unpacked.js
unpackedChallenge.js
utils.js
docs
guide
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.french.md
README.italian.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env

90 lines
2.6 KiB
Markdown
Raw Normal View History

---
id: ae9defd7acaf69703ab432ea
title: Smallest Common Multiple
isRequired: true
challengeType: 5
---
## Description
<section id='description'>
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.
The range will be an array of two numbers that will not necessarily be in numerical order.
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers <em>between</em> 1 and 3. The answer here would be 6.
Remember to use <a href='http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514' target='_blank'>Read-Search-Ask</a> if you get stuck. Try to pair program. Write your own code.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>smallestCommons([1, 5])</code> should return a number.
testString: assert.deepEqual(typeof smallestCommons([1, 5]), 'number', '<code>smallestCommons([1, 5])</code> should return a number.');
- text: <code>smallestCommons([1, 5])</code> should return 60.
testString: assert.deepEqual(smallestCommons([1, 5]), 60, '<code>smallestCommons([1, 5])</code> should return 60.');
- text: <code>smallestCommons([5, 1])</code> should return 60.
testString: assert.deepEqual(smallestCommons([5, 1]), 60, '<code>smallestCommons([5, 1])</code> should return 60.');
- text: <code>smallestCommons([2, 10])</code> should return 2520.
testString: assert.deepEqual(smallestCommons([2, 10]), 2520, '<code>smallestCommons([2, 10])</code> should return 2520.');
- text: <code>smallestCommons([1, 13])</code> should return 360360.
testString: assert.deepEqual(smallestCommons([1, 13]), 360360, '<code>smallestCommons([1, 13])</code> should return 360360.');
- text: <code>smallestCommons([23, 18])</code> should return 6056820.
testString: assert.deepEqual(smallestCommons([23, 18]), 6056820, '<code>smallestCommons([23, 18])</code> should return 6056820.');
```
</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'>
```js
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);
}
```
</section>