Function doesn't return value?

Does any know why is my helper function not returning anything, it keeps showing up “undefined”.

I am recalling the helper function if the number is more than 1 digits and if the number is only 1 digit then the function should just return some value.

Here is my code:

function persistence(num) {
  let numOfTimesToMultiply = 0;

  const multiplyHelper = (multiplyNum) => {
    
    if (multiplyNum.toString().length === 1) {
      // If the number is only 1 degit it should return "numOfTimesToMultiply" value
      return numOfTimesToMultiply;
      
    } else {
      // If the number is more than 1 degits, run below codes   
      numOfTimesToMultiply++;
      const multiplyNumArray = multiplyNum
        .toString()
        .split("")
        .map((indivMultiplyNum) => parseInt(indivMultiplyNum));
      const totalNumber = multiplyNumArray.reduce((totalNum, indivNum) => {
        totalNum *= indivNum;
        return totalNum;
      });
      
      // and call the helper function again       
      multiplyHelper(totalNumber);
    }
  };
  
  // Should console log number 3   
  console.log(multiplyHelper(num));
}

persistence(55);

what you are doing with this value?

?? Sorry, I still don’t quite get it :pensive:
I thought even though the number becomes only 1 digit, it’ll still call itself again but when the function gets here

if (multiplyNum.toString().length === 1) {
  return numOfTimesToMultiply;
}

it will stop running the function and return the “numOfTimesToMultiply” value.

but what are you doing with the value returned from this?

this function call returns a vale but you are not collecting it in any way

2 Likes