알고리즘 42 - How good are you really?

jabae·2021년 10월 28일
0

알고리즘

목록 보기
42/97

Q.

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!

A)

function betterThanAverage(classPoints, yourPoints) {
  classPoints.push(yourPoints);
  return (classPoints.reduce((sum, cur) => sum + cur))
  		/ classPoints.length < yourPoints ? true : false
}

other

나는 .push로 배열에 넣어주고 작업했는데, .reudce를 할 때 초깃값으로 내 점수를 넣고, 나누기에 배열의 길이 + 1 하는 방법도 있다.

function betterThanAverage(classPoints, yourPoints) {
  return yourPoints > classPoints.reduce(function(sum, x){return sum + x},yourPoints)/(classPoints.length+1)
}
profile
it's me!:)

0개의 댓글