알고리즘, 자료구조

[프로그래머스] 성격 유형 검사하기

Developer Mobssie 2022. 8. 19. 15:28

 

 

잇몸으로 윽깬 나의 코드

function solution(survey, choices){
    let answer = ''
    let selectType = ''
    let score = [0, 3, 2, 1, 0, 1, 2, 3]
    let surveyType = ['RT','CF','JM','AN']
    let surveyList = surveyType.join('').split('')
    let surveyMap = new Map()
    
    for(let i = 0; i < surveyList.length; i++){
        surveyMap.set(surveyList[i], 0)
    }
    for(let i = 0; i < survey.length; i++){
        if(choices[i] > 4) selectType = survey[i].charAt(1)
        else if(choices[i] < 4) selectType = survey[i].charAt(0)
        surveyMap.set(selectType, surveyMap.get(selectType) + score[choices[i]])
    }
    
    answer = surveyType.map(v => {
        const keys = v.split('')
        return (surveyMap.get(keys[0]) || 0) >= (surveyMap.get(keys[1]) || 0) ? keys[0] : keys[1]
    })
    
    return answer.join('');
}
const survey = ['AN', 'CF', 'MJ', 'RT', 'NA']
const choices = [5, 3, 2, 7, 5]
solution(survey, choices)

 

 

 

 

참조하고 수정한 다른 코드

function solution(survey, choices){
    let answer = ''
    let surveyType = ['RT','CF','JM','AN']
    let surveyList = surveyType.join('').split('')
    
    var surveyMap = new Map()
    for(let i = 0; i < surveyList.length; i++){
        surveyMap.set(surveyList[i], 0)
    }
    choices.forEach((score, i) => {
        let [A, B] = survey[i].split('');
        if(score > 4) surveyMap.set(B, surveyMap.get(B)+score-4);
        else if(score < 4) surveyMap.set(A, surveyMap.get(A)+4-score);
    })
    answer = surveyType.map(v => {
        const keys = v.split('')
        return (surveyMap.get(keys[0]) || 0) >= (surveyMap.get(keys[1]) || 0) ? keys[0] : keys[1]
    })
    
    return answer.join('');
}
const survey = ['AN', 'CF', 'MJ', 'RT', 'NA']
const choices = [5, 3, 2, 7, 5]
solution(survey, choices)

j