기록하는 개발자

[프로그래머스][코딩테스트 고득점 Kit][Javascript] 모의고사 본문

Algorithm

[프로그래머스][코딩테스트 고득점 Kit][Javascript] 모의고사

밍맹030 2023. 8. 13. 02:32
728x90

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

전체 코드

function solution(answers) {
    let answer = [];
    let first = [1, 2, 3, 4, 5];
    let second = [2, 1, 2, 3, 2, 4, 2, 5];
    let third = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
    let correct = [0,0,0];
    
    for(let i=0; i<answers.length; i++){
        if(first[i%5]==answers[i]) correct[0]++;
        if(second[i%8]==answers[i]) correct[1]++;
        if(third[i%10]==answers[i]) correct[2]++; 
    }
    
    let max = Math.max(...correct);
    
    for(let i=0; i<correct.length; i++){
        if(correct[i]==max) answer.push(i+1);
    }
    return answer;
}
728x90