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

[Codewars] How good are you really? (8 kyu) / JavaScript

by fluss 2022. 10. 25.

https://www.codewars.com/kata/5601409514fc93442500010b

 

Codewars - Achieve mastery through coding practice and developer mentorship

Coding practice 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:

There was a test in your class and you passed it. Congratulations!
But you're an ambitious person. You want to know if you're better than the average student in your class.

You receive an array with your peers' test scores. Now calculate the average and compare your score!

Return True if you're better, else False!

 

Note:

Your points are not included in the array of your class's points. For calculating the average point you may add your point to the given array!

 

설명:

당신의 반에 시험이 있었고 당신은 시험을 통과했습니다. 축하합니다!

하지만 당신은 야심이 있는 사람입니다. 당신이 반의 평균 학생들보다 나은지 알고 싶습니다.

동료들의 시험 점수가 배열로 주어집니다. 평균을 계산하고 당신의 점수와 비교하세요! 당신이 우수하다면 True를 그렇지 않다면 False를 반환하세요!

 

참고:

당신의 점수는 반 점수의 배열에 포함되지 않습니다. 평균을 계산하기 위해 당신의 점수를 주어진 배열에 더할 수 있습니다!

 

풀이

reduce로 배열의 모든 원소를 더하고 배열의 길이로 나누어 평균을 구했다. 그리고 yourPoint가 평균보다 크면 true를 작으면 false를 반환했다.

 

코드

function betterThanAverage(classPoints, yourPoints) {
  let avg = (classPoints.reduce((a, b) => a + b, 0)) / classPoints.length;
  if(yourPoints > avg) return true;
  else return false;
}