문제
https://school.programmers.co.kr/learn/courses/30/lessons/118666
function solution(survey, choices) {
//정답 변수
let answer = '';
//성격 유형 점수를 성격 유형에 따라 정리할 배열
const personalityType = [
{'R': 0, 'T': 0},
{'C': 0, 'F': 0},
{'J': 0, 'M': 0},
{'A': 0, 'N': 0},
];
//선택지에 따른 배점
const score = {
1: 3,
2: 2,
3: 1,
4: 0,
5: 1,
6: 2,
7: 3
}
for(let i = 0; i < survey.length; i++) {
//survey 원소에 따라 personalityType에 인덱스를 정리해준다.
let personalIdx = 0;
if(survey[i][0] === 'R' || survey[i][1] === 'R') {
personalIdx = 0;
} else if(survey[i][0] === 'C' || survey[i][1] === 'C'){
personalIdx = 1;
} else if(survey[i][0] === 'J' || survey[i][1] === 'J'){
personalIdx = 2;
} else{
personalIdx = 3;
}
//personalityType 성격 유형에 choices 원소를 score에서 찾아서 값을 더 해준다.
if(choices[i] < 4) {
personalityType[personalIdx][survey[i][0]] += score[choices[i]];
} else if(choices[i] > 4) {
personalityType[personalIdx][survey[i][1]] += score[choices[i]];
}
}
//personalityType배열안에 들어있는 각 객체마다 value(점수)만 추출한 뒤 그 중 가장 큰 값(점수)의 key(성격 유형)를 리턴해주는 함수
const findMaxPersonality = (obj) =>{
const objValues = Object.values(obj)
const maxScore = Math.max(...objValues);
for(let key in obj){
if(maxScore === obj[key]){
return key;
}
}
}
for(let i = 0; i< personalityType.length; i++){
//findMaxPersonality 함수를 통해 각 지표마다 점수가 가장 높은 성격유형들을 answer 문자열에 더해준다.
const maxPersonality = findMaxPersonality(personalityType[i]);
answer += maxPersonality;
}
return answer;
}
느낀점
카카오 문제라 그런지 1단계 치고는 난이도가 좀 있는 편이다.
객체로 정리해서 풀면 어렵지 않게 풀 수 있다.