PersistentBurger

samuel Jo·2023년 6월 9일
0

codewars

목록 보기
21/47

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 39 = 27, 27 = 14, 14 = 4 and 4 has only one digit)
999 --> 4 (because 9
99 = 729, 729 = 126, 126 = 12, and finally 12 = 2)
4 --> 0 (because 4 is already a one-digit number)

// 첫번째 짠 코드 
function persistence(num){
	// return할 횟수
    let count=0;
    while(num >=10){
    	num = String(num).split("").reduce((a,b)=>a*b);
        count++;
    }
    return count;
}


//두번째 짠 코드
function persistence(num){
	for(let i=0; num >9; i++){
    	num=num.toString().split("").reduce((a,b)=>a*b);
    }
    //i가 결국 횟수를 의미하니까.
    return i;
}
profile
step by step

0개의 댓글