From d624fe5843465b111e41c882a7da71e1a7f7acb0 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Wed, 20 Dec 2017 17:58:36 +0530 Subject: [PATCH 1/4] Add gcdOfArray --- snippets/gcdOfArray.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 snippets/gcdOfArray.md diff --git a/snippets/gcdOfArray.md b/snippets/gcdOfArray.md new file mode 100644 index 000000000..4865ebb14 --- /dev/null +++ b/snippets/gcdOfArray.md @@ -0,0 +1,12 @@ +### gcdOfArray + +It finds the GCD of all the numbers in an array by using `array.reduce` and the fact that `gcd(a,b,c) = gcd(gcd(a,b),c)` + +```js +const gcdOfArray = arr => + { + const gcd = (x, y) => !y ? x : gcd(y, x % y); + arr.reduce((a,b) => gcd(a,b)) + } +// functionName(sampleInput) -> sampleOutput +``` From 1b3a70f2e043b95924fbcff065cdf1aea828f5fb Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Wed, 20 Dec 2017 18:07:11 +0530 Subject: [PATCH 2/4] Add test cases for gcdOfArray --- snippets/gcdOfArray.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/snippets/gcdOfArray.md b/snippets/gcdOfArray.md index 4865ebb14..dde3bfd08 100644 --- a/snippets/gcdOfArray.md +++ b/snippets/gcdOfArray.md @@ -6,7 +6,8 @@ It finds the GCD of all the numbers in an array by using `array.reduce` and the const gcdOfArray = arr => { const gcd = (x, y) => !y ? x : gcd(y, x % y); - arr.reduce((a,b) => gcd(a,b)) + return arr.reduce((a,b) => gcd(a,b)) } -// functionName(sampleInput) -> sampleOutput +// gcdOfArray([1,2,3,4,5]) -> 1 +// gcdOfArray([4,8,12]) -> 4 ``` From 3970afa1cfee4a604c30cb684e77a502c8672868 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Wed, 20 Dec 2017 19:29:42 +0530 Subject: [PATCH 3/4] indent the code correctly --- snippets/gcdOfArray.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/gcdOfArray.md b/snippets/gcdOfArray.md index dde3bfd08..33e671aba 100644 --- a/snippets/gcdOfArray.md +++ b/snippets/gcdOfArray.md @@ -1,10 +1,10 @@ + ### gcdOfArray -It finds the GCD of all the numbers in an array by using `array.reduce` and the fact that `gcd(a,b,c) = gcd(gcd(a,b),c)` +It finds the GCD of all the numbers in an array by using `Array.reduce()` and the fact that `gcd(a,b,c) = gcd(gcd(a,b),c)` ```js -const gcdOfArray = arr => - { +const gcdOfArray = arr =>{ const gcd = (x, y) => !y ? x : gcd(y, x % y); return arr.reduce((a,b) => gcd(a,b)) } From ea1a3004611b1b12dada06f9c6243efd5d4e3b1b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 20 Dec 2017 16:12:32 +0200 Subject: [PATCH 4/4] Update and rename gcdOfArray.md to arrayGcd.md Updated description and naming for consistency. --- snippets/arrayGcd.md | 14 ++++++++++++++ snippets/gcdOfArray.md | 13 ------------- 2 files changed, 14 insertions(+), 13 deletions(-) create mode 100644 snippets/arrayGcd.md delete mode 100644 snippets/gcdOfArray.md diff --git a/snippets/arrayGcd.md b/snippets/arrayGcd.md new file mode 100644 index 000000000..39a154059 --- /dev/null +++ b/snippets/arrayGcd.md @@ -0,0 +1,14 @@ +### arrayGcd + +Calculates the greatest common denominator (gcd) of an array of numbers. + +Use `Array.reduce()` and the `gcd` formula (uses recursion) to calculate the greatest common denominator of an array of numbers. + +```js +const arrayGcd = arr =>{ + const gcd = (x, y) => !y ? x : gcd(y, x % y); + return arr.reduce((a,b) => gcd(a,b)); +} +// arrayGcd([1,2,3,4,5]) -> 1 +// arrayGcd([4,8,12]) -> 4 +``` diff --git a/snippets/gcdOfArray.md b/snippets/gcdOfArray.md deleted file mode 100644 index 33e671aba..000000000 --- a/snippets/gcdOfArray.md +++ /dev/null @@ -1,13 +0,0 @@ - -### gcdOfArray - -It finds the GCD of all the numbers in an array by using `Array.reduce()` and the fact that `gcd(a,b,c) = gcd(gcd(a,b),c)` - -```js -const gcdOfArray = arr =>{ - const gcd = (x, y) => !y ? x : gcd(y, x % y); - return arr.reduce((a,b) => gcd(a,b)) - } -// gcdOfArray([1,2,3,4,5]) -> 1 -// gcdOfArray([4,8,12]) -> 4 -```