본문 바로가기
알고리즘/Codewars

[Codewars] Grasshopper - Grade book (8 kyu) / JavaScript

by fluss 2023. 2. 13.

https://www.codewars.com/kata/55cbd4ba903825f7970000f5

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Grade book

Complete the function so that it finds the average of the three scores passed to it and returns the letter value associated with that grade.

Numerical ScoreLetter Grade
90 <= score <= 100 'A'
80 <= score < 90 'B'
70 <= score < 80 'C'
60 <= score < 70 'D'
0 <= score < 60 'F'

Tested values are all between 0 and 100. Theres is no need to check for negative values or values greater than 100.

 

코드

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

 

다른 사람의 좋았던 풀이

function getGrade (s1, s2, s3) {
  var s = (s1 + s2 + s3) / 3
  return s >= 90 ? "A" : s >= 80 ? "B" : s >= 70 ? "C" : s >= 60 ? "D" : "F"
}

댓글