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

Commit

Permalink
feat: add LCM function (#185)
Browse files Browse the repository at this point in the history
* Add LCM function

* Add index and package

* Fix lint issue

* Revert package.json changes
  • Loading branch information
lfidelino authored and Kent C. Dodds committed Aug 20, 2018
1 parent e65bd26 commit 97551ae
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@
"kentcdodds/ava"
]
}
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import generatePassword from './generate-password'
import tail from './tail'
import makeObjectIterable from './makeObjectIterable'
import fibonacciSum from './fibonacciSum'
import lcm from './lcm'

export {
reverseArrayInPlace,
Expand Down Expand Up @@ -136,4 +137,5 @@ export {
tail,
makeObjectIterable,
fibonacciSum,
lcm,
}
23 changes: 23 additions & 0 deletions src/lcm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default lcm

/**
* Original source: https://stackoverflow.com/a/38407734
*
* This method returns least common multiple of two integers
*
* @param {Number} a - first integer
* @param {Number} b - second integer
* @return {Number} - least common multiple
*/

function gcd(a, b) {
if (b === 0) {
return a
} else {
return gcd(b, a % b)
}
}

function lcm(a, b) {
return a * b / gcd(a, b)
}
18 changes: 18 additions & 0 deletions test/lcm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test from 'ava'
import {lcm} from '../src'

test('Get lcm of two unequal integers', t => {
const a = 8
const b = 12
const expected = 24
const actual = lcm(a, b)
t.deepEqual(actual, expected)
})

test('Get lcm of equal integers', t => {
const a = 11
const b = 11
const expected = 11
const actual = lcm(a, b)
t.deepEqual(actual, expected)
})

0 comments on commit 97551ae

Please sign in to comment.