Currently, I am working on the Persistent Bugger Kata and have come up with the following code. Although it successfully logs the correct value, I am facing difficulty in properly returning the final result. My approach involves using recursion.
const persistence = num => {
x = num.toString();
x = x.split('');
console.log(x);
if (x.length === 1) {
return parseInt(x.join());
// console.log(`num is ${x}`);
// return x;
}
if (x.length > 1) {
len = x.length;
arr = [];
for (i = 0; i < x.length; i++) {
if (i === 0) {
arr.push(x[i]);
} else {
arr.push(arr[0] * x[i]);
arr.shift();
}
num = parseInt(arr.join());
}
persistence(num);
}
};
I am striving to retrieve the final output.