나의 답안

function solution(survey, choices) {
    let map = {"R":0,"T":0,"C":0,"F":0,"J":0,"M":0,"A":0,"N":0}
    let word = ["R","T","C","F","J","M","A","N"]
    var answer = '';
    for(i in survey){
        if(choices[i]<4){
            map[survey[i][0]] +=Math.abs(choices[i]-4)
        }else if(choices[i]>4){
            map[survey[i][1]] += choices[i]-4
        }
    }
    
    for(i=0; i<=6; i+=2){
        if(map[word[i]]<map[word[i+1]]){
            answer += word[i+1]
        }else{
            answer += word[i]
        }
    }    
    return answer;
}

다른사람의 풀이

function solution(survey, choices) {
    const data = { R: 0, T: 0, C: 0, F: 0, J: 0, M: 0, A: 0, N: 0 }

    for (let i = 0; i < survey.length; i++) {
        const score = choices[i] - 4
        let type = survey[i].split('')[score < 0 ? 0 : 1] 
        data[type] += Math.abs(score)
    }

    const { R, T, C, F, J, M, A, N } = data
    return `${R >= T ? 'R' : 'T'}${C >= F ? 'C' : 'F'}${J >= M ? 'J' : 'M'}${A >= N ? 'A' : 'N'}`
}

 

+ Recent posts