forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrongNumber.js
More file actions
Latest commit
21 lines (19 loc) · 593 Bytes
/
Copy pathArmstrongNumber.js
File metadata and controls
21 lines (19 loc) · 593 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Author: dephraiim
* License: GPL-3.0 or later
*
* An Armstrong number is equal to the sum of its own digits each raised to the power of the number of digits.
* For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
* An Armstrong number is often called Narcissistic number.
*
*/
constarmstrongNumber=(num)=>{
if(typeofnum!=='number'||num<0)returnfalse
constnumStr=num.toString()
constsum=[...numStr].reduce(
(acc,digit)=>acc+parseInt(digit)**numStr.length,
0
)
returnsum===num
}
export{armstrongNumber}