I was trying to make a function that calculates the multiplicative persistence on my own. It works when I pass in a number that has a multiplicative persistence value of 2 or less.
But when passing in a number whose multiplicative persistence is more than 2, it is returning an incorrect value.
Here is the function I wrote:
function persistence(num) {
let res = 10
let count=0
const helper = (number)=>{
res=1
const digits = number.toString().split('')
for(let digit of digits){
res = res* Number(digit)
}
count++
console.log(res,count)
return
}
if(num<10) return 0
if(res>=10){
if(count===0) helper(num)
helper(res)
}
return count
}