taesik

persistence function 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. 본문

functions and trick/typescript_javascript

persistence function 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.

taesikk 2022. 1. 24. 20:19

my solution :

const persistence = num => {
  return `${num}`.length > 1 
    ? 1 + persistence(`${num}`.split('').reduce((a, b) => a * +b)) 
    : 0;
}

 

 

 

 

const persistence = num => {
  return `${num}`.length > 1 
    ? 1 + persistence(`${num}`.split('').reduce((a, b) => a * +b)) 
    : 0;
}

'functions and trick > typescript_javascript' 카테고리의 다른 글

isAscending  (0) 2022.01.25
diamond with *  (0) 2022.01.24
2D array of number. to find the sum of minimum value in each row.  (0) 2022.01.24
has the same amount of 'x's and 'o's.  (0) 2022.01.23
contain_all_rotation  (0) 2022.01.23