Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function persistence(num) {
- let digits = num.toString();
- if (digits.length === 1) {
- return 0;
- }
- let calculate =1;
- let counter = 0;
- while (digits.length > 1) {
- for (let i = 0; i < digits.length; i++) {
- calculate *= digits[i];
- }
- digits = String(calculate);
- calculate = 1;
- counter++;
- }
- return counter;
- }
- console.log(persistence(39));
- /* Description:
- Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
- For example (Input --> Output):
- 39 --> 3 (because 3*9 = 27, 2*7 = 14, 1*4 = 4 and 4 has only one digit, there are 3 multiplications)
- 999 --> 4 (because 9*9*9 = 729, 7*2*9 = 126, 1*2*6 = 12, and finally 1*2 = 2, there are 4 multiplications)
- 4 --> 0 (because 4 is already a one-digit number, there is no multiplication)
- */
Add Comment
Please, Sign In to add comment