프로그래머스 Lv.1 성격유형검사하기

Kim Jason·2023년 4월 3일
0

알고리즘 노트

목록 보기
14/35
post-thumbnail

2022 카카오 테크 인턴십

💁🏻 코드

function solution(survey, choices) {
    const typeScore = { R: 0, T: 0, C: 0, F: 0, J: 0, M: 0, A: 0, N: 0 };
    const types = ['RT', 'CF', 'JM', 'AN'];
    
    choices.forEach((choice, idx) => {
        const [x, o] = survey[idx];
        typeScore[choice > 4 ? o : x] += Math.abs(choice - 4);
    });
    
    return types.map(([a, b]) => typeScore[b] > typeScore[a] ? b : a).join('');
}

입력값의 제한은 다음과 같다.

  • 1 <= survey, choices 배열의 길이 <= 1,

이중 for문 사용까지는 무방하다고 생각했다.

✨ 한 가지 사실을 짚고 넘어가자면, 다음처럼 변수를 선언할 수 있다!

const exStr = 'ab';
const [str1, str2] = exStr;
console.log(str1);  // ✅ 'a'
console.log(str2);  // ✅ 'b'
profile
성장지향형 프론트엔드 개발자

0개의 댓글