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

[Codewars] Total amount of points (8 kyu) / JavaScript

by fluss 2023. 2. 19.

https://www.codewars.com/kata/5bb904724c47249b10000131

 

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:

Our football team has finished the championship.

Our team's match results are recorded in a collection of strings. Each match is represented by a string in the format "x:y", where x is our team's score and y is our opponents score.

For example: ["3:1", "2:2", "0:1", ...]

Points are awarded for each match as follows:

  • if x > y: 3 points (win)
  • if x < y: 0 points (loss)
  • if x = y: 1 point (tie)

We need to write a function that takes this collection and returns the number of points our team (x) got in the championship by the rules given above.

Notes:

  • our team always plays 10 matches in the championship
  • 0 <= x <= 4
  • 0 <= y <= 4

 

코드

function points(games) {
  return games.reduce((a, b) => {
    let [x, y] = b.split(':');
    let n = 0;
    if(x > y) n = 3;
    if(x < y) n = 0;
    if(x === y) n = 1;
    return a + n;
  }, 0)
}

댓글