fix: optimised armstrongNumber code (#1374)

This commit is contained in:
Ayush shah
2023-09-30 17:35:27 +05:30
committed by GitHub
parent 394483bff7
commit 964ba049d7

View File

@ -9,16 +9,13 @@
*/
const armstrongNumber = (num) => {
if (num < 0 || typeof num !== 'number') return false
let newSum = 0
const numArr = num.toString().split('')
numArr.forEach((num) => {
newSum += parseInt(num) ** numArr.length
})
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
}
export { armstrongNumber }