Skip to content

Commit 964ba04

Browse files
fix: optimised armstrongNumber code (TheAlgorithms#1374)
1 parent 394483b commit 964ba04

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

‎Maths/ArmstrongNumber.js‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
*/
1010

1111
constarmstrongNumber=(num)=>{
12-
if(num<0||typeofnum!=='number')returnfalse
13-
14-
letnewSum=0
15-
16-
constnumArr=num.toString().split('')
17-
numArr.forEach((num)=>{
18-
newSum+=parseInt(num)**numArr.length
19-
})
20-
21-
returnnewSum===num
12+
if(typeofnum!=='number'||num<0)returnfalse
13+
constnumStr=num.toString()
14+
constsum=[...numStr].reduce(
15+
(acc,digit)=>acc+parseInt(digit)**numStr.length,
16+
0
17+
)
18+
returnsum===num
2219
}
2320

2421
export{armstrongNumber}

0 commit comments

Comments
(0)