-
Notifications
You must be signed in to change notification settings - Fork 1
/
DEV#174.js
241 lines (209 loc) · 6.01 KB
/
DEV#174.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//https://dev.to/thepracticaldev/daily-challenge-174-soccer-league-table-2393
class LeagueTable {
constructor(){
this.matches = []
}
push = (matchStr) => {
const teams = matchStr.split(' - ');
const homeTeam = teams[0].slice(0,-2);
const awayTeam = teams[1].slice(2);
const homeGoals = parseInt(teams[0].slice(-2));
const awayGoals = parseInt(teams[1].slice(0, 2));
let homeScore = 0;
let awayScore = 0;
if(homeGoals == awayGoals) {
homeScore = 1;
awayScore = 1;
} else if(homeGoals > awayGoals){
homeScore = 3;
} else {
awayScore = 0;
}
const match = {
homeTeam,
awayTeam,
homeGoals,
awayGoals,
homeScore,
awayScore,
};
this.matches.push(match);
return JSON.stringify(match);
};
get_points = (teamName) => {
let points = 0;
this.matches.forEach(match => {
if(match.homeTeam === teamName){
points += match.homeScore;
} else if(match.awayTeam === teamName){
points += match.awayScore;
}
})
return points;
};
get_goals_for = (teamName) => {
let goals = 0;
this.matches.forEach(match => {
if(match.homeTeam === teamName){
goals += match.homeGoals;
} else if(match.awayTeam === teamName){
goals += match.awayGoals;
}
});
return goals;
};
get_goals_against = (teamName) => {
let goals = 0;
this.matches.forEach(match => {
if(match.homeTeam === teamName){
goals += match.awayGoals;
} else if(match.awayTeam === teamName){
goals += match.homeGoals;
}
});
return goals;
};
get_goals_difference = (teamName) => {
let diff = 0;
this.matches.forEach(match => {
if(match.homeTeam === teamName){
diff += (match.homeGoals - match.awayGoals);
} else if(match.awayTeam === teamName){
diff += (match.awayGoals - match.homeGoals);;
}
});
return diff;
};
get_wins = (teamName) => {
let wins = 0;
this.matches.forEach(match => {
if(match.homeTeam === teamName && match.homeScore === 3){
wins++
} else if(match.awayTeam === teamName && match.awayScore === 3){
wins++
}
});
return wins;
};
get_draws = (teamName) => {
let draws = 0;
this.matches.forEach(match => {
if(match.homeTeam === teamName && match.homeScore === 1){
draws++
} else if(match.awayTeam === teamName && match.awayScore === 1){
draws++
}
});
return draws;
};
get_losses = (teamName) => {
let losses = 0;
this.matches.forEach(match => {
if(match.homeTeam === teamName && match.homeScore === 0){
losses++
} else if(match.awayTeam === teamName && match.awayScore === 0){
losses++
}
});
return losses;
};
};
worldCup2020 = new LeagueTable();
//Tests
const cases = [
{
label: 'Man Utd 2 - 1 Liverpool',
input: worldCup2020.push('Man Utd 2 - 1 Liverpool'),
shouldBe: JSON.stringify({
homeTeam: 'Man Utd',
awayTeam: 'Liverpool',
homeGoals: 2,
awayGoals: 1,
homeScore: 3,
awayScore: 0
})
},
{
label: 'Liverpool 3 - 0 Man Utd',
input: worldCup2020.push('Liverpool 3 - 0 Man Utd'),
shouldBe: JSON.stringify({
homeTeam: 'Liverpool',
awayTeam: 'Man Utd',
homeGoals: 3,
awayGoals: 0,
homeScore: 3,
awayScore: 0
})
},
{
label: 'Get Liverpool points',
input: worldCup2020.get_points('Liverpool'),
shouldBe: 3
},
{
label: 'Get Man Utd points',
input: worldCup2020.get_points('Man Utd'),
shouldBe: 3
},
{
label: 'Get Liverpool goals',
input: worldCup2020.get_goals_for('Liverpool'),
shouldBe: 4
},
{
label: 'Get Man Utd goals',
input: worldCup2020.get_goals_for('Man Utd'),
shouldBe: 2
},
{
label: 'Get goals against Liverpool',
input: worldCup2020.get_goals_against('Liverpool'),
shouldBe: 2
},
{
label: 'Get goals against Man Utd',
input: worldCup2020.get_goals_against('Man Utd'),
shouldBe: 4
},
{
label: 'Get goals difference Liverpool',
input: worldCup2020.get_goals_difference('Liverpool'),
shouldBe: 2
},
{
label: 'Get goals difference Man Utd',
input: worldCup2020.get_goals_difference('Man Utd'),
shouldBe: -2
},
{
label: 'Get wins Liverpool',
input: worldCup2020.get_wins('Liverpool'),
shouldBe: 1
},
{
label: 'Get wins Man Utd',
input: worldCup2020.get_wins('Man Utd'),
shouldBe: 1
},
{
label: 'Get losses Liverpool',
input: worldCup2020.get_losses('Liverpool'),
shouldBe: 1
},
{
label: 'Get losses Man Utd',
input: worldCup2020.get_losses('Man Utd'),
shouldBe: 1
},
{
label: 'Get draws Liverpool',
input: worldCup2020.get_draws('Liverpool'),
shouldBe: 0
},
{
label: 'Get draws Man Utd',
input: worldCup2020.get_draws('Man Utd'),
shouldBe: 0
}
]
cases.map(test => console.log((test.input == test.shouldBe ? '✅' : '❗') + " " + test.label + " => " + "Should be: " + test.shouldBe + " received: " + test.input))