Does my number look big in this?
Dec 1, 2022 | Code, JavaScriptInstructions from CodeWars challenge read:
A Narcissistic Number (or Armstrong Number) is a positive number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this Kata, we will restrict ourselves to decimal (base 10).
For example, take 153 (3 digits), which is narcisstic:
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
and 1652 (4 digits), which isn't:
1^4 + 6^4 + 5^4 + 2^4 = 1 + 1296 + 625 + 16 = 1938
The Challenge:
Your code must return true or false (not 'true' and 'false') depending upon whether the given number is a Narcissistic number in base 10.
My Solution
First, I convert the passed in number to a string. This allows me to iterate over the number to split the digits into an array. Map the values and use parseInt(x, 10) to convert each back to an integer.
Using Array's reduce method, we do the entire calculation in one go and simply return true or false if this new value equals the value that was originally passed in.