From 4e38ab5331b6cf6f53c0477563ee4a8318ac02b8 Mon Sep 17 00:00:00 2001 From: shahayush480 Date: Sat, 30 Sep 2023 14:40:27 +0530 Subject: [PATCH] fix: #758 optimised armstrongNumber code --- Maths/ArmstrongNumber.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Maths/ArmstrongNumber.js b/Maths/ArmstrongNumber.js index a3ab78299f..8f861d8c15 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 } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy