(프로그래머스) [1차] 다트 게임

유지원·2022년 4월 13일
0

프로그래머스

목록 보기
26/66

문제 링크

https://programmers.co.kr/learn/courses/30/lessons/17682?language=javascript


Javascript

function solution(dartResult) {
    const bonus = { 'S': 1, 'D': 2, 'T': 3 },
          options = { '*': 2, '#': -1, undefined: 1 };

    let darts = dartResult.match(/\d.?\D/g);

    for (let i = 0; i < darts.length; i++) {
        let split = darts[i].match(/(^\d{1,})(S|D|T)(\*|#)?/),
            score = Math.pow(split[1], bonus[split[2]]) * options[split[3]];

        if (split[3] === '*' && darts[i - 1]) darts[i - 1] *= options['*'];

        darts[i] = score;
    }

    return darts.reduce((a, b) => a + b);
}
profile
👋 https://github.com/ujw0712

0개의 댓글