mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
fix: optimised armstrongNumber code (#1374)
This commit is contained in:
@ -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 }
|
||||
|
Reference in New Issue
Block a user