Skip to content
Next Next commit
feat: 성격 유형 검사하기, 기존풀이에 추가
  • Loading branch information
codeisneverodd committed Aug 29, 2022
commit 450ec5b2a5b0458755f6ff6b7672b6f6c6fcbd5c
31 changes: 29 additions & 2 deletions level-1/성격-유형-검사하기.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//https://github.com/codeisneverodd/programmers-coding-test
//더 좋은 풀이가 존재할 수 있습니다.

//정답 1 - ssi02014
function solution(survey, choices) {
const points = [3, 2, 1, 0, 1, 2, 3];
Expand Down Expand Up @@ -42,4 +41,32 @@ function solution(survey, choices) {
}

return result;
}
}

//정답 2 - codeisneverodd
function solution(survey, choices) {
const points = [-3, -2, -1, 0, 1, 2, 3];
const types = ['RT', 'CF', 'JM', 'AN'];
const pointBoard = types.reduce((a, key) => ({ ...a, [key]: 0 }), {});
survey.forEach((key, i) => {
const point = points[choices[i] - 1];
Object.keys(pointBoard).includes(key) ? (pointBoard[key] += point) : (pointBoard[key[1] + key[0]] -= point);
});
return types.map(type => (pointBoard[type] <= 0 ? type[0] : type[1])).join('');
}

//정답 3 - codisneverodd
function solution(survey, choices) {
const points = [-3, -2, -1, 0, 1, 2, 3];
const types = ['RT', 'CF', 'JM', 'AN'];
const pointBoard = survey.reduce((a, key, i) => {
if (types.includes(key)) {
a[key] = (a[key] || 0) + points[choices[i] - 1];
return a;
}
const rKey = key[1] + key[0];
a[rKey] = (a[rKey] || 0) - points[choices[i] - 1];
return a;
}, {});
return types.map(type => (pointBoard[type] <= 0 || !pointBoard[type] ? type[0] : type[1])).join('');
}