From 9c11215714253427f60df21ca0f01159dc21bb80 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 01:33:54 +0530 Subject: [PATCH 1/5] Snippet for checking armstrong number --- snippets/isArmstrongNumber.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 snippets/isArmstrongNumber.md diff --git a/snippets/isArmstrongNumber.md b/snippets/isArmstrongNumber.md new file mode 100644 index 000000000..d9a96be1e --- /dev/null +++ b/snippets/isArmstrongNumber.md @@ -0,0 +1,17 @@ +### isArmstrongNumber + +Checks if the given number is an armstrong number or not. + +Convert the given number into array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`. + +```js +const isArmstrongNumber = digits =>{ + let total = 0, arr = (digits+"").split(""); + arr.map(d =>total += Math.pow(parseInt(d), arr.length)) + if(total === digits) return true; return false; +} + +// isArmstrongNumber(1634) -> true +// isArmstrongNumber(371) -> true +// isArmstrongNumber(56) -> false +``` \ No newline at end of file From 504a4c5c459f54c3bb9544db525ce67bdefc45fc Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 10:11:03 +0530 Subject: [PATCH 2/5] Added space after arrow --- snippets/isArmstrongNumber.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/isArmstrongNumber.md b/snippets/isArmstrongNumber.md index d9a96be1e..4925f3dc8 100644 --- a/snippets/isArmstrongNumber.md +++ b/snippets/isArmstrongNumber.md @@ -5,13 +5,13 @@ Checks if the given number is an armstrong number or not. Convert the given number into array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`. ```js -const isArmstrongNumber = digits =>{ +const isArmstrongNumber = digits => { let total = 0, arr = (digits+"").split(""); - arr.map(d =>total += Math.pow(parseInt(d), arr.length)) + arr.map(d => total += Math.pow(parseInt(d), arr.length)) if(total === digits) return true; return false; } // isArmstrongNumber(1634) -> true // isArmstrongNumber(371) -> true // isArmstrongNumber(56) -> false -``` \ No newline at end of file +``` From f1e28ed4343bb9655717d145b41492c55c5a6673 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 12:08:18 +0530 Subject: [PATCH 3/5] Updated function --- snippets/isArmstrongNumber.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/snippets/isArmstrongNumber.md b/snippets/isArmstrongNumber.md index 4925f3dc8..d934dd715 100644 --- a/snippets/isArmstrongNumber.md +++ b/snippets/isArmstrongNumber.md @@ -5,10 +5,9 @@ Checks if the given number is an armstrong number or not. Convert the given number into array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`. ```js -const isArmstrongNumber = digits => { - let total = 0, arr = (digits+"").split(""); - arr.map(d => total += Math.pow(parseInt(d), arr.length)) - if(total === digits) return true; return false; +const isArmstrongNumber = num => { + let arr = (num+"").split(""); + return arr.reduce((a, d) => parseInt(a) + Math.pow(parseInt( d ), arr.length), 0) == num ? true : false } // isArmstrongNumber(1634) -> true From 5c4041d3941283ae50987c6c1a1d5a09bc4d1d6a Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 12:30:56 +0530 Subject: [PATCH 4/5] Updated function with help from @skatcat31 --- snippets/isArmstrongNumber.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/snippets/isArmstrongNumber.md b/snippets/isArmstrongNumber.md index d934dd715..2b9f2970a 100644 --- a/snippets/isArmstrongNumber.md +++ b/snippets/isArmstrongNumber.md @@ -5,10 +5,7 @@ Checks if the given number is an armstrong number or not. Convert the given number into array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`. ```js -const isArmstrongNumber = num => { - let arr = (num+"").split(""); - return arr.reduce((a, d) => parseInt(a) + Math.pow(parseInt( d ), arr.length), 0) == num ? true : false -} +const isArmstrongNumber = digits => ( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) ) // isArmstrongNumber(1634) -> true // isArmstrongNumber(371) -> true From 3c66ced74ef2a3fcae9dadc95858cb029933fe3c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 20 Dec 2017 11:10:54 +0200 Subject: [PATCH 5/5] Update isArmstrongNumber.md --- snippets/isArmstrongNumber.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/isArmstrongNumber.md b/snippets/isArmstrongNumber.md index 2b9f2970a..be8857784 100644 --- a/snippets/isArmstrongNumber.md +++ b/snippets/isArmstrongNumber.md @@ -5,8 +5,8 @@ Checks if the given number is an armstrong number or not. Convert the given number into array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`. ```js -const isArmstrongNumber = digits => ( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) ) - +const isArmstrongNumber = digits => + ( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) ); // isArmstrongNumber(1634) -> true // isArmstrongNumber(371) -> true // isArmstrongNumber(56) -> false