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)
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Array plus array (8 kyu) / JavaScript (0) | 2023.02.21 |
---|---|
[Codewars] Rock Paper Scissors! (8 kyu) / JavaScript (0) | 2023.02.20 |
[Codewars] Count the Monkeys! (8 kyu) / JavaScript (0) | 2023.02.18 |
[Codewars] Remove the minimum (7 kyu) / JavaScript (0) | 2023.02.17 |
[Codewars] Do I get a bonus? (8 kyu) / JavaScript (0) | 2023.02.16 |
댓글