[프로그래머스] 3진법 뒤집기 (javascript)

728x90
반응형

three를 3진법 변수를 문자열로 썼지만 배열로 선언해서 넣으면 split()을 안써도 된다. 

편한대로 하시길

function solution(n) {
    var answer = 0;
    let three='';
    while(n){
        let r=n%3;
        three=r+three;
        n=Math.floor(n/3);
    }
    //뒤집을 필요없다 1*(3의 0승) + 2*(3의1승)+... 이기 때문에
    return three.split('').reduce((acc,cur,idx)=>{
        return acc+Number(cur)*3**idx
    },0)

}
728x90
반응형
TAGS.

Comments