성격유형검사하기

이묘·2022년 12월 17일
0

CodingTest

목록 보기
32/41
post-thumbnail
function solution(survey, choices) {
  const test = {};

  for (var i = 0; i < survey.length; i++) {

    if (choices[i] < 4) {
      if (test[survey[i][0]] == undefined) {
        test[survey[i][0]] = 4-choices[i];
      }
      else{
        test[survey[i][0]] += 4-choices[i];
      }

    } 
    else if (choices[i] > 4) {
      if (test[survey[i][1]] == undefined) {
        test[survey[i][1]] = choices[i]-4;
      }
      else{
        test[survey[i][1]] += choices[i]-4;
      }
    }
  }

  var answer = "";

  const mbtiList = ['RT', 'CF', 'JM', 'AN'];

  for (var i = 0; i < mbtiList.length; i++) {
    if(test[mbtiList[i][0]] == undefined){
      test[mbtiList[i][0]] = 0
    }
    if(test[mbtiList[i][1]] == undefined){
      test[mbtiList[i][1]] = 0
    }

    if(test[mbtiList[i][0]] >= test[mbtiList[i][1]]){
      answer += mbtiList[i][0]
    }
    else{
      answer += mbtiList[i][1]
    }

    
  }

  return answer;
}

console.log(solution(
  ["AN", "CF", "MJ", "RT", "NA"],	[5, 3, 2, 7, 5]
));

더 짧게 리펙토링 할 수 있으니 다음에 까먹을 때 즈음에 한 번 더 해볼 것..!!!

profile
본질을 공부해야 응용도 하지 않을까

0개의 댓글