본문 바로가기
알고리즘

[알고리즘] Javascript 로또의 최고 순위와 최저 순위

by 개발 까마귀 2021. 9. 17.
반응형

프로그래머스 로또의 최고 순위와 최저 순위

function solution(lottos, win_nums) {    
    const answers = [];

    const lottoRank = {
        '6': 1,
        '5': 2,
        '4': 3,
        '3': 4,
        '2': 5,
        '1': 6,
        '0': 6,
    };

    let mathLottoIndex = 0;
    let zeroLottoIndex = 0;

    for (const lottoNumber of lottos) {
        const matchStatus = win_nums.includes(lottoNumber);

        if (matchStatus) mathLottoIndex++;
        else if (lottoNumber === 0) zeroLottoIndex++;
    }

    const best = lottoRank[mathLottoIndex + zeroLottoIndex];
    const worst = lottoRank[
                    mathLottoIndex === 6 
                    ? 6 
                    : mathLottoIndex + zeroLottoIndex - zeroLottoIndex];

    answers.push(best, worst);

    return answers;
}

 

function solution(lottos, win_nums) {
    const answers = [];

    const lottoRank = {
        '6': 1,
        '5': 2,
        '4': 3,
        '3': 4,
        '2': 5,
        '1': 6,
        '0': 6,
    };

    const matchLotto = lottos.filter((lottoNumber) => win_nums.includes(lottoNumber)).length;
    const zeroLotto = lottos.filter((lottoNumber) => !lottoNumber).length;

    const best = lottoRank[matchLotto + zeroLotto];
    const worst = lottoRank[matchLotto === 6 ? 6 : matchLotto + zeroLotto - zeroLotto];

    answers.push(best, worst);

    return answers;
}

 

감사합니다.

반응형

댓글