Recursive function returns undefined instead of the number

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++){
     return count;
     let l=digits.reduce((a,b)=>{
     return a*b;
let h=persistence(39)

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

Can you give more details about how you have debugged so far?

Your code seems to have two competing solution approaches?

You shouldn’t be trying to cheat the recursion by dumping values in the global space. That breaks reusabiltiy of the function.

This is the breaking condition of recursive function
I used console.log (count) it works fine count value is 3,
But when it returns to variable h it doesn’t have a value of 3

  1. why aren’t you using the return value of the recursive call

  2. why are you mixing and matching the string and array approaches

  3. what will happen if someone tries to use your function twice

1 Like

thanks, I try to change my logic.

How to increment count for every recursion without using global space .How should I do it. any hint?

You should use the return value of the recursive function call.

Alternatively, you could rework your logic to use a while loop instead of recursion.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.