# javascript – Multiplication of numbers in an array, n times

## Question:

I want to do a function that multiplies the digits of a number until they give a result less than 10 and it returns the number of multiplications of the digits of the number passed by parameter that were carried out …

Ex: function (234) = 2 // because 2 * 3 * 4 = 24, 2 * 4 = 8

Ex: function (49) = 3 // because 4 * 9 = 36, 3 * 6 = 18, 1 * 8 = 8

` `function multiplicacionPersistente(num) { let multiplicacion = String(num).split('').map(numeros => ( Number(numeros) )).reduce((anterior,actual) =>( anterior*actual )); return multiplicacion } var respuesta = multiplicacionPersistente(999) console.log(respuesta);``

It would be something like doing this function of mine but many times .. recursive … thanks

You have the base case: the number is less than 10 (one digit):

``````function multiplicaciones(n) {
if (n<10) { return 0; }
...
}
``````

The case to iterate over would be when n> 10:

``````...
else {
return 1 + multiplicaciones(String(n).split('').map(s => +s).reduce((acc,n)=> n*acc));
}
...
}
``````

all this gives:

` `function multiplicaciones(n) { if (n<10) { console.log('no hay que multiplicar', n); return 0; } else { console.log('Multiplicando los dígitos de',n); return 1 + multiplicaciones(String(n).split('').map(s => +s).reduce((acc,n)=> n*acc)); } } console.log('Numero de multiplicaciones para 99:',multiplicaciones(99)); console.log('Numero de multiplicaciones para 555:',multiplicaciones(555));``
Scroll to Top