We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 394483b commit 964ba04Copy full SHA for 964ba04
Maths/ArmstrongNumber.js
@@ -9,16 +9,13 @@
9
*/
10
11
const armstrongNumber = (num) => {
12
- if (num < 0 || typeof num !== 'number') return false
13
-
14
- let newSum = 0
15
16
- const numArr = num.toString().split('')
17
- numArr.forEach((num) => {
18
- newSum += parseInt(num) ** numArr.length
19
- })
20
21
- return newSum === num
+ if (typeof num !== 'number' || num < 0) return false
+ const numStr = num.toString()
+ const sum = [...numStr].reduce(
+ (acc, digit) => acc + parseInt(digit) ** numStr.length,
+ 0
+ )
+ return sum === num
22
}
23
24
export { armstrongNumber }
0 commit comments