From e67005f052b68a00bb03349d69adb979dd6580f0 Mon Sep 17 00:00:00 2001 From: atomiks Date: Fri, 5 Jan 2018 00:39:22 +1100 Subject: [PATCH] Update gcd.md --- snippets/gcd.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/snippets/gcd.md b/snippets/gcd.md index f450bbfa1..34e5635cd 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -2,15 +2,14 @@ Calculates the greatest common divisor between two or more numbers/arrays. -The `helperGcd `function uses recursion. +The inner `_gcd `function uses recursion. Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`. ```js const gcd = (...arr) => { - let data = [].concat(...arr); - const helperGcd = (x, y) => (!y ? x : gcd(y, x % y)); - return data.reduce((a, b) => helperGcd(a, b)); + const _gcd = (x, y) => (!y ? x : gcd(y, x % y)); + return [].concat(...arr).reduce((a, b) => _gcd(a, b)); }; ```