[Lv.0] 성격 유형 검사하기

01수정·2023년 11월 14일
0

문제








풀이

  • 차근차근 풀었더니 성공했다! 너무 기쁘다!!!! 최적의 방법은 아닌 것 같지만 소요시간도 그리 길지 않다.
function solution(survey, choices) {
    const types = [['R', 'T'], ['C', 'F'], ['J', 'M'], ['A', 'N']]
    const scores = { 1:3, 2:2, 3:1, 4:0, 5:1, 6:2, 7:3 }
    
    let result = {}
    let answer = ''
    
    // 설문을 통한 각 type 에 대한 점수 
    survey.forEach((sv, idx) => {
        const first = sv.charAt(0)
        const second = sv.charAt(1)
        if (choices[idx] < 4) {
            result[first] = (result[first] || 0) + scores[choices[idx]]
        } else if (choices[idx] > 4) {
            result[second] = (result[second] || 0) + scores[choices[idx]]
        } else {
            
        }
    })

    // 점수 > 순서로 최종 type 뽑아내기 
    types.forEach((type, idx) => {
        const score_of_first = result[type[0]] || 0;
        const score_of_second = result[type[1]] || 0;

        if (score_of_first === 0 && score_of_second === 0) {
            answer += type[0];
        } else {
            answer += (score_of_first >= score_of_second ? type[0] : type[1]);
        }
    })
    
    return answer;
}

다른 풀이

function solution(survey, choices) {
    const MBTI = {};
    const types = ["RT","CF","JM","AN"];

    types.forEach((type) =>
        type.split('').forEach((char) => MBTI[char] = 0)
    )

    choices.forEach((choice, index) => {
        const [disagree, agree] = survey[index];

        MBTI[choice > 4 ? agree : disagree] += Math.abs(choice - 4);
    });

    return types.map(([a, b]) => MBTI[b] > MBTI[a] ? b : a).join("");
}
profile
새싹 FE 개발자

0개의 댓글