I have the following problem:
Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
function persistence(num) {
let count = 0;
let numStr = num.toString();
if (numStr.length === 1){
return 0
}
if (numStr.length === 2){
while (numStr.length > 1){
count += 1
numStr = (Number(numStr[0])*Number(numStr[1])).toString()
}
}
if (numStr.length === 3){
while (numStr.length > 1){
count += 1
numStr = (Number(numStr[0])*Number(numStr[1])*Number(numStr[2])).toString()
}
}
return count
}
persistence(999) //solution 4
I'm getting "Execution Timed Out (12000 ms)" error. I know there's different ways to tackle this problem but I want to know specifically what is wrong with my code.
Aucun commentaire:
Enregistrer un commentaire