persistence(999) => 4 //Потому что 9*9*9 = 729, 7 * 2 * 9 = 126,
//1 * 2 * 6 = 12, 1 * 2 = 0, функция умножила число 4 раза
const persistence = (number, i = 0) => {
const digits = number.toString().split('');
const result = digits.reduce((acc, el) => acc * el);
if(result > 9) {
return persistence(result, ++i);
}
return ++i;
}
function persistence(num) {
function multiply(n) {
return n.reduce(function(a, b) {
return a * b;
});
}
var count = 0;
while (num.toString().length > 1) {
num = num.toString().split("");
num = multiply(num);
count++;
}
return count;
}
console.log(persistence(999));
let count = 0;
const persistence = num => {
count = 0;
const numbers = [...num.toString()];
const result = numbers.reduce((n, next) => Number(n) * next, 1);
if (num > 9) {
persistence(result);
count++;
}
return count;
};
persistence(999); // => 4