From 31f5f7d162fcdae44386bbb9f1da96ceceacac9f Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 01:33:54 +0530 Subject: [PATCH] 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