-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023-12-07 Codewars 8 Kyu Fundamentals - Total Amount of Points.js
61 lines (43 loc) · 1.43 KB
/
2023-12-07 Codewars 8 Kyu Fundamentals - Total Amount of Points.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 12/7/23 Thursday Codewars 8 Kyu Fundamentals - Total Amount of Points
// https://www.codewars.com/kata/5bb904724c47249b10000131/train/javascript
/*
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) {
let teamPoints = 0;
for(let i = 0; i < games.length; i++) {
// Split match into array of scores
const [team1Score, team2Score] = games[i].split(':')
if(team1Score > team2Score) {
teamPoints += 3;
} else if(team1Score === team2Score) {
teamPoints += 1;
}
}
return teamPoints;
}
/* =============
Other Solutions
============= */
function points(games) {
var sum=0;
for (var i=0; i<games.length; ++i)
{
if (games[i][0]>games[i][2])
sum+=3;
if (games[i][0]==games[i][2])
sum+=1;
}
return sum;
}