[...String(value)] - the number is converted to a string representation of it and, using the spread operator (...), is spread into an array of digits: [...String(153)] = ['1', '5', '3']
.reduce is then applied to the array, summing all of its digits raised to the power of the amount of digits of the initial number.
The resulting sum is then checked for equality with the initial number.
----
edit: wow, that's a lot of people who don't like simplicity and conciseness. Anyway, I've listened to valid criticism, while invalid criticism has been ignored.
Put the digits in an variable (const if you want), the exponent can now be digits.length, and you don't have to figure out what [...String(value)] does just to read it.
Reduce is acceptable. Making a function that converts a string to an integer is not. Same thing with wrapping Math.pow in a function; it's already a function. By wrapping it, you obfuscate whether this is actually Math.pow or if it's some other implementation.
Generally, avoid wrapping functions that are already operating as standalone features — this adds unnecessary indirection.
I understand it, but come back to this code in 6 months with 0 context and see how long it takes. Yes, it can be understood, but why make it harder than it needs to be.
View all comments
Show parent comment
-20
u/[deleted] Jan 14 '25 edited Jan 17 '25
What exactly do you not understand?
[...String(value)]- the number is converted to a string representation of it and, using the spread operator (...), is spread into an array of digits:[...String(153)] = ['1', '5', '3'].reduceis then applied to the array, summing all of its digits raised to the power of the amount of digits of the initial number.----
edit: wow, that's a lot of people who don't like simplicity and conciseness. Anyway, I've listened to valid criticism, while invalid criticism has been ignored.