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.
let count=0;
function persistence(num) {
//code me\
let digits=[]
let d=num.toString().length
for(let i=1;i<=d;i++){
digits.push(num%10)
num=Math.floor(num/10)
}
if(digits.length==1){
return count;
}else{
count++
let l=digits.reduce((a,b)=>{
return a*b;
})
persistence(l)
}
}
let h=persistence(39)
console.log(h)
Within a recursive function, the incremental count variable for each iteration but when returned, it does not return the number stored in the count but rather returns undefined. Can someone help me figure this out? this problem is persistent bugger of codewars