From c244042665a33e75f0f07268e669c766320bfae0 Mon Sep 17 00:00:00 2001 From: Rohit Date: Thu, 21 Dec 2017 20:35:45 +0530 Subject: [PATCH 1/2] Add snippet arrayLcm --- snippets/arrayLcm.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets/arrayLcm.md diff --git a/snippets/arrayLcm.md b/snippets/arrayLcm.md new file mode 100644 index 000000000..60a2c94d8 --- /dev/null +++ b/snippets/arrayLcm.md @@ -0,0 +1,15 @@ +### arrayLcm + +Calculates the lowest common multiple (lcm) of an array of numbers. + +Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the lowest common multiple of an array of numbers. + +```js +const arrayLcm = arr =>{ + const gcd = (x, y) => !y ? x : gcd(y, x % y); + const lcm = (x, y) => (x*y)/gcd(x, y) + return arr.reduce((a,b) => lcm(a,b)); +} +// arrayLcm([1,2,3,4,5]) -> 60 +// arrayLcm([4,8,12]) -> 12 +``` \ No newline at end of file From 16cba8a454c2262953734d7637dac30057dbee7c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 21 Dec 2017 17:27:07 +0200 Subject: [PATCH 2/2] Update arrayLcm.md Fixed second example as @iamsoorena suggested --- snippets/arrayLcm.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/arrayLcm.md b/snippets/arrayLcm.md index 60a2c94d8..5439d5924 100644 --- a/snippets/arrayLcm.md +++ b/snippets/arrayLcm.md @@ -11,5 +11,5 @@ const arrayLcm = arr =>{ return arr.reduce((a,b) => lcm(a,b)); } // arrayLcm([1,2,3,4,5]) -> 60 -// arrayLcm([4,8,12]) -> 12 -``` \ No newline at end of file +// arrayLcm([4,8,12]) -> 24 +```