[프로그래머스 문제풀이12]모의고사 자바스크립트

매드갈릭레드망고·2021년 9월 15일
0
function solution(answers) {
    var answer = [];
    const length=answers.length;
    const theirAnswers=[makeOne(length),makeTwo(length),makeThree(length)];
    for(const theirAnswer of theirAnswers){
        answer.push(check(theirAnswer,answers))
    }
    answer=isWinner(answer)
    return answer;
}
function isWinner(answer){
    let max=0;
    let winners=[]
    for(const number of answer){
        if(number>max){
            max=number
        }   
    }
    answer.map((num,i)=>{
        if(num===max)
            return winners.push(i+1);
    })
    winners.sort((a,b)=>{return a-b});
    return winners
}
function check(theirAnswer,answers){
    let result=0;
    answers.map((answer,i)=>{
            
            if(answer===theirAnswer[i])
                result+=1;
        })
   
    return result;
}
function makeOne(length){
    const result=[]
    for(let i=0;i<length;i++){
        result.push((i)%5+1);
    }
    return result;
}
function makeTwo(length){
    const result=[];
    const pattern=[3,5,6,7];
    let nowPattern=3;
    for(let i=0; i<length;i++){
        if(i%2===0){
            result.push(2);
        }else{
            result.push(nowPattern-2);
            nowPattern=pattern[((i+1)/2)%4]
        }
    }
    return result;
}
function makeThree(length){
     const result=[];
    const pattern=[3,1,2,4,5];
    let nowNum=3;
      for(let i=0; i<length;i++){
        if(i%2===0){
            result.push(nowNum);
        }else{
            result.push(nowNum);
            nowNum=pattern[(i+1)/2%5]
        }
    }
    return result
}

패턴찾느라 시간이 좀 걸린 문제 패턴만찾으면 쉽다.

0개의 댓글