mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
fix: optimised armstrongNumber code (#1374)
This commit is contained in:
@ -9,16 +9,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const armstrongNumber = (num) => {
|
const armstrongNumber = (num) => {
|
||||||
if (num < 0 || typeof num !== 'number') return false
|
if (typeof num !== 'number' || num < 0) return false
|
||||||
|
const numStr = num.toString()
|
||||||
let newSum = 0
|
const sum = [...numStr].reduce(
|
||||||
|
(acc, digit) => acc + parseInt(digit) ** numStr.length,
|
||||||
const numArr = num.toString().split('')
|
0
|
||||||
numArr.forEach((num) => {
|
)
|
||||||
newSum += parseInt(num) ** numArr.length
|
return sum === num
|
||||||
})
|
|
||||||
|
|
||||||
return newSum === num
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { armstrongNumber }
|
export { armstrongNumber }
|
||||||
|
Reference in New Issue
Block a user