getGrade.js (8kyu 83)

Codewars 알고리즘 풀이


Problem

  • Complete the function so that it finds the mean of the three scores passed to it and returns the letter value associated with that grade.
    • 세 가지 score의 평균을 찾고, 해당 성적과 관련된 문자 값을 반환한다.
  • Tested values are all between 0 and 100.
    • 테스트된 값은 모두 0에서 100 사이의 숫자이다.


90 <= score <= 100: A

80 <= score < 90: B

70 <= score < 80: C

60 <= score < 70: D

0 <= score < 60: F


Solution 01

function getGrade(s1, s2, s3) {
  let average = (s1 + s2 + s3) / 3;
  
  if (average < 60) {
    return 'F';
  } else if (average < 70) {
    return 'D';
  } else if (average < 80) {
    return 'C';
  } else if (average < 90) {
    return 'B';
  } else if (average <= 100) {
    return 'A';
  } else {
    return 'Error';
  }
}

getGrade(92, 93, 94);  // A
getGrade(82, 83, 84);  // B
getGrade(72, 73, 74);  // C
getGrade(62, 63, 64);  // D
getGrade(52, 53, 54);  // F


Solution 02

function getGrade(s1, s2, s3) {
  let average = (s1 + s2 + s3) / 3;
  
  if (average < 60) return 'F';
  else if (average < 70) return 'D';
  else if (average < 80) return 'C';
  else if (average < 90) return 'B';
  else if (average <= 100) return 'A';
  else return 'Error';
}

getGrade(92, 93, 94);  // A
getGrade(82, 83, 84);  // B
getGrade(72, 73, 74);  // C
getGrade(62, 63, 64);  // D
getGrade(52, 53, 54);  // F


Solution 03

function getGrade(s1, s2, s3) {
  let average = (s1 + s2 + s3) / 3;
  
  return average < 60 ? 'F'
    : average < 70 ? 'D'
    : average < 80 ? 'C'
    : average < 90 ? 'B'
    : average <= 100 ? 'A'
    : 'Error';
}

getGrade(92, 93, 94);  // A
getGrade(82, 83, 84);  // B
getGrade(72, 73, 74);  // C
getGrade(62, 63, 64);  // D
getGrade(52, 53, 54);  // F