Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   helpi !!! помогите решить тестовое на курсы (https://javascript.ru/forum/misc/80770-helpi-pomogite-reshit-testovoe-na-kursy.html)

Paulinka 30.07.2020 18:55

helpi !!! помогите решить тестовое на курсы
 
:help:

(JS)..Дописать функцию getPersistence, которая принимает не отрицательное число number и возвращает его мультипликативную устойчивость. Чтобы посчитать мультипликативную устойчивость, нужно умножать цифры даного числа между собой, пока результат не будет содержать только одну цифру.

'use strict';

/**
* @param {Number} number
*
* @returns {Number}
*/

function getPersistence(number) {

// write code here

}


:cray:
Пример:

getPersistence(39) === 3 (3*9 = 27, 2*7 = 14, 1*4 = 4)
getPersistence(1000) === 1 (1*0*0*0 = 0)
getPersistence(5389) === 2 (5*3*8*9 = 1080, 1*0*8*0 = 0)
getPersistence(4) === 0

безмерно благодарна за отклик!! :thanks:

рони 30.07.2020 20:07

Paulinka,
:( :( :(
<script>
const getPersistence = (num, i = 0)=> (num = num > 9 ? (i++,Array.from(num.toString()).reduce((a,b) => a * b)) : 0, num > 9 ? getPersistence(num, i) : i);
document.write(getPersistence(39))
    </script>

Paulinka 30.07.2020 23:43

мерси :thanks:
:write:

рони 27.02.2021 08:54

:write:
<script>
function getPersistence(number) {
    let i = 0;
    while (number > 9) {
        i++;
        let [a, ...b] = number.toString();
        for (let k of b) a *= k;
        number = a
    }
    return i
}
document.write(getPersistence(39))
   </script>


Часовой пояс GMT +3, время: 11:38.