Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 97551ae

Browse files
lfidelinoKent C. Dodds
authored and
Kent C. Dodds
committed
feat: add LCM function (#185)
* Add LCM function * Add index and package * Fix lint issue * Revert package.json changes
1 parent e65bd26 commit 97551ae

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@
101101
"kentcdodds/ava"
102102
]
103103
}
104-
}
104+
}

Diff for: src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import generatePassword from './generate-password'
6666
import tail from './tail'
6767
import makeObjectIterable from './makeObjectIterable'
6868
import fibonacciSum from './fibonacciSum'
69+
import lcm from './lcm'
6970

7071
export {
7172
reverseArrayInPlace,
@@ -136,4 +137,5 @@ export {
136137
tail,
137138
makeObjectIterable,
138139
fibonacciSum,
140+
lcm,
139141
}

Diff for: src/lcm.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default lcm
2+
3+
/**
4+
* Original source: https://stackoverflow.com/a/38407734
5+
*
6+
* This method returns least common multiple of two integers
7+
*
8+
* @param {Number} a - first integer
9+
* @param {Number} b - second integer
10+
* @return {Number} - least common multiple
11+
*/
12+
13+
function gcd(a, b) {
14+
if (b === 0) {
15+
return a
16+
} else {
17+
return gcd(b, a % b)
18+
}
19+
}
20+
21+
function lcm(a, b) {
22+
return a * b / gcd(a, b)
23+
}

Diff for: test/lcm.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import test from 'ava'
2+
import {lcm} from '../src'
3+
4+
test('Get lcm of two unequal integers', t => {
5+
const a = 8
6+
const b = 12
7+
const expected = 24
8+
const actual = lcm(a, b)
9+
t.deepEqual(actual, expected)
10+
})
11+
12+
test('Get lcm of equal integers', t => {
13+
const a = 11
14+
const b = 11
15+
const expected = 11
16+
const actual = lcm(a, b)
17+
t.deepEqual(actual, expected)
18+
})

0 commit comments

Comments
 (0)