Jun 개발노트

프로그래머스 - 콜라츠추측

January 14, 2020

[콜라츠추측](https://programmers.co.kr/learn/courses/30/lessons/12943

const collatzGuess = num => {
    let count = 0;
    const getFinalCount = x => {
        let answer = 0;
        if (x === 1 || count > 500) return;
        answer = x % 2 === 0 ? x / 2 : x * 3 + 1;
        count++;
        return getFinalCount(answer);
    };
    getFinalCount(num);
    return count > 500 ? -1 : count;
};

test("콜라츠 추측", () => {
    expect(collatzGuess(626331)).toEqual(8);
});

Written by Junho You 배운것을 기록하자