From 5a8bb6712442e460495dc0a7bda3f0eb5a6eb7ba Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Tue, 7 Sep 2021 17:55:11 +0530 Subject: [PATCH] fix the string method problem --- Maths/CheckKishnamurthyNumber.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Maths/CheckKishnamurthyNumber.js b/Maths/CheckKishnamurthyNumber.js index ab512bfb8..bb283e68e 100644 --- a/Maths/CheckKishnamurthyNumber.js +++ b/Maths/CheckKishnamurthyNumber.js @@ -29,10 +29,14 @@ const CheckKishnamurthyNumber = (number) => { // create a variable to store the sum of all digits factorial. let sumOfAllDigitFactorial = 0 // convert the number to string for convenience. - String(number).split('').map(digit => { - // split one by one digit and calculate factorial and store to the variable. - return (sumOfAllDigitFactorial += factorial(Number(digit))) - }) + let newNumber = number + // Extract number digits using the remainder method. + while (newNumber > 0) { + const lastDigit = newNumber % 10 + // calculate each digit factorial. + sumOfAllDigitFactorial += factorial(lastDigit) + newNumber = Math.floor(newNumber / 10) + } // if the sumOftheFactorial is equal to the given number it means the number is a Krishnamurthy number. return sumOfAllDigitFactorial === number }