Skip to content

Commit 5a8bb67

Browse files
fix the string method problem
1 parent 800308f commit 5a8bb67

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Maths/CheckKishnamurthyNumber.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ const CheckKishnamurthyNumber = (number) => {
2929
// create a variable to store the sum of all digits factorial.
3030
let sumOfAllDigitFactorial = 0
3131
// convert the number to string for convenience.
32-
String(number).split('').map(digit => {
33-
// split one by one digit and calculate factorial and store to the variable.
34-
return (sumOfAllDigitFactorial += factorial(Number(digit)))
35-
})
32+
let newNumber = number
33+
// Extract number digits using the remainder method.
34+
while (newNumber > 0) {
35+
const lastDigit = newNumber % 10
36+
// calculate each digit factorial.
37+
sumOfAllDigitFactorial += factorial(lastDigit)
38+
newNumber = Math.floor(newNumber / 10)
39+
}
3640
// if the sumOftheFactorial is equal to the given number it means the number is a Krishnamurthy number.
3741
return sumOfAllDigitFactorial === number
3842
}

0 commit comments

Comments
 (0)