Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

278
Views
Recursive approach to Persistent bugger problem returns undefined

I've been trying to solve the following problem in codewars using recursion:

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. For example (Input --> Output):

39 --> 3 (because 3*9 = 27, 2*7 = 14, 1*4 = 4 and 4 has only one digit)
999 --> 4 (because 9*9*9 = 729, 7*2*9 = 126, 1*2*6 = 12, and finally 1*2 = 2)
4 --> 0 (because 4 is already a one-digit number)

Here's what I've tried:

var numOfIterations = 0;
function persistence(num) {
   //code me
  var i;
  var digits=[];
  var result = 1;
  if (num.toString().length==1) {
    return numOfIterations;
  } else {
      numOfIterations++;
      digits = Array.from(String(num), Number);
      for (i=0;i<digits.size;i++) {
        result=result*digits[i];
      }
      persistence(result);
    }
}

But for some reason, instead of returning the number of iterations, it returns undefined. I've been told that I'm not using recursion correctly, but I just can't find the problem.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Other answers have explained what's wrong with your code. I just want to point out a simpler implementation:

const multiplyDigits = (n) =>
  n < 10 ? n : (n % 10) * multiplyDigits (n / 10 | 0);

const persistence = (n) =>
  n < 10 ? 0 : 1 + persistence (multiplyDigits (n));

[39, 999, 4] .forEach (t => console .log (`${t}:\t${persistence (t)}`));

multiplyDigits does just what it says, recursively multiplying the final digit by the number left when you remove that last digit (Think of | 0 as like Math .floor), and stopping when n is a single digit.

persistence checks to see if we're already a single digit, and if so, returns zero. If not, we add one to the value we get when we recur on the multiple of the digits.

about 4 years ago · Juan Pablo Isaza Report

0

As @David wrote in his answer, you were missing the return of the recursive call to itself.

Plus you were using digits.size instead of digits.length.

Anyway consider that one single digit being zero will collpse the game because that's enough to set the result to zero despite how many digits the number is made of.

To deal with the reset of numOfIterations, at first I tried using function.caller to discriminate between recursive call and direct call and set the variable accordingly. Since that method is deprecated as shown here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller

I opted for the optional argument iteration that gets set to zero as default, to keep track of that value while it goes down the call stack. This solution still fulfills the fact that the caller doesn't need to know a new interface for the function to work.

//var numOfIterations = 0;

function persistence(num, iteration=0) {

  /*
  Commented strategy using the function.caller
  working but deprecated so I can't recommend anymore
  used optional argument iteration instead
  
  //gets the name of the caller scope
  let callerName = persistence.caller?.name;
  //if it's different from the name of this function
  if (callerName !== 'persistence')
    //reset the numOfIterations
    numOfIterations = 0;
  */
     
  var digits=[];  
  if (num.toString().length==1){  
    return iteration;    
  } else {    
    var result = 1;    
    digits = Array.from(String(num), Number);
    for (let i=0;i<digits.length;i++) {
      result = result * digits[i];
    }
    return persistence(result, iteration+1);
  }
}

console.log( persistence(39) );        //-> 3
console.log( persistence(999 ) );      //-> 4
console.log( persistence(4) );         //-> 0

about 4 years ago · Juan Pablo Isaza Report

0

I've been told that I'm not using recursion correctly

You're recursing, but you're not returning the result of that recursion. Imagine for a moment just this structure:

function someFunc() {
  if (someCondition) {
    return 1;
  } else {
    anotherFunc();
  }
}

If someCondition is false, what does someFunc() return? Nothing. So it's result is undefined.

Regardless of any recursion, at its simplest if you want to return a result from a function then you need to return it:

function persistence(num) {
  //...
  if (num.toString().length==1) {
    //...
  } else {
    //...
    return persistence(result); // <--- here
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!