diff --git a/Maths/ArmstrongNumber.js b/Maths/ArmstrongNumber.js index a3ab78299..8f861d8c1 100644 --- a/Maths/ArmstrongNumber.js +++ b/Maths/ArmstrongNumber.js @@ -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 }