[프로그래머스] 다트 게임🤣

GoGoDev·2021년 6월 28일
0

Programmers Lv.1🌱

목록 보기
43/46

https://programmers.co.kr/learn/courses/30/lessons/17682

function solution(dartResult) {
    var answer = 0;
    var temp = [];
    for(let i = 0; i < dartResult.length; i++){
        if(isNaN(dartResult[i]) === false){
            if(dartResult[i+1] == 0){
                if(dartResult[i+2] === "S"){
                    temp.push(10**1);
                }
                else if(dartResult[i+2] === "D"){
                    temp.push(10**2);
                }
                else if(dartResult[i+2] === "T"){
                    temp.push(10**3);
                }
            }
            else if(dartResult[i+1] === "S"){
                temp.push(dartResult[i]**1);
            }
            else if(dartResult[i+1] === "D"){
                temp.push(dartResult[i]**2);
            }
            else if(dartResult[i+1] === "T"){
                temp.push(dartResult[i]**3);
            }
        }
        if(dartResult[i]==='*'){
            if(i >= 2 && i <= 5){
                for(let j = 0; j < temp.length; j++){
                    temp[j] = temp[j]*2;
                }
            } else if(i > 5){
                for(let j = 1; j < temp.length; j++){
                    temp[j] = temp[j]*2;
                }
            }    
        } else if(dartResult[i]==='#'){
            if(i == 2)
                temp[0] = temp[0]*(-1);
            if(i > 2 && i <= 5){
                for(let j = 1; j < temp.length; j++){
                    temp[j] = temp[j]*(-1);
                }
            } else if(i > 5){
                for(let j = 2; j < temp.length; j++){
                    temp[j] = temp[j]*(-1);
                }
            }   
        }
    }
    console.log(temp)
    for(let i = 0; i < temp.length; i++){
        answer += temp[i]
    }
    return answer;
}

정규식으로 풀기

코드를 입력하세요
profile
🐣차근차근 무럭무럭🐣

0개의 댓글