본문 바로가기
코딩 테스트/프로그래머스

[프로그래머스 - Javascript] Lv.1 모의고사

by 오엥?은 2023. 2. 7.
반응형
  • 문제

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

 

프로그래머스

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

programmers.co.kr

 

  • 풀이
function solution(answers) {
    one = [1, 2, 3, 4, 5];
    two = [2, 1, 2, 3, 2, 4, 2, 5];
    three = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
    let countOne = 0;
    let countTwo = 0;
    let countThree = 0;

    for (let i = 0; i < answers.length; i++) {
        // 1번
        if (answers[i] === one[(i % 5)])
            countOne++;    
        // 2번
        if (answers[i] === two[(i % 8)])
            countTwo++; 
        // 3번
        if (answers[i] === three[(i % 10)])
            countThree++;
    }
    let num = Math.max(countOne, countTwo, countThree);
    let result = [];
    
    if (countOne === num) result.push(1);    
    if (countTwo === num) result.push(2);
    if (countThree === num) result.push(3);
    
    return result;
}

 

반응형