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);