-
Notifications
You must be signed in to change notification settings - Fork 0
/
codChallenges.js
167 lines (145 loc) · 3.45 KB
/
codChallenges.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
// Fist challenge
const game = {
team1: {
name: "Bayern Munich",
},
team2: { name: "Borrussia Dortmund" },
players: [
[
"Neuer",
"Pavard",
"Martinez",
"Alaba",
"Davies",
"Kimmich",
"Goretzka",
"Coman",
"Muller",
"Gnarby",
"Lewandowski",
],
[
"Burki",
"Schulz",
"Hummels",
"Akanji",
"Hakimi",
"Weigl",
"Witsel",
"Hazard",
"Brandt",
"Sancho",
"Gotze",
],
],
score: "4:0",
scored: ["Lewandowski", "Gnarby", "Lewandowski", "Hummels"],
date: "Nov 9th, 2037",
odds: {
team1: 1.33,
x: 3.25,
team2: 6.5,
},
};
// 1
const [players1, players2] = game.players;
// 2
[game.team1.gk, ...game.team1.fieldPlayers] = players1;
[game.team2.gk, ...game.team2.fieldPlayers] = players2;
console.log(game.team1.gk);
console.log(game.team2.gk);
console.log(game.team1.fieldPlayers);
console.log(game.team2.fieldPlayers);
// 3
const allPlayers = players1.concat(players2);
// console.log(allPlayers);
// 4
//First approach, not good
// const players1Final = players1.push("Thiago", "Coutinho", "Perisic");
// console.log(players1Final);
// console.log(players1);
const players1Final = players1.concat("Thiago", "Coutinho", "Perisic");
//console.log(players1Final);
// 5
const { team1, x: draw, team2 } = game.odds;
// 6
const countGoals = function (listScorers, playerName) {
count = 0;
for (let i = 0; i < listScorers.length; i++) {
if (listScorers[i] == playerName) {
count++;
}
}
return count;
};
// console.log(game.scored);
// console.log(countGoals(game.scored, "Lewandowski"));
const printGoals = function (...players) {
for (let i = 0; i < players.length; i++) {
console.log(players[i], countGoals(game.scored, players[i]));
}
};
printGoals("Gotze", "Hummels", "Lewandowski", "Kimmich");
// this works but I understood the problem wrong
const printGoals2 = function (...players) {
console.log(players);
console.log(`${players.length} goals were scored`);
};
printGoals2(...game.scored);
// 7
game.odds.team1 < game.odds.team2 &&
console.log("Team 1 is more likely to win");
// Second challenge
// 1
let theResult = `the goals were: `;
for (const [i, player] of game.scored.entries()) {
theResult += `Goal ${i}: ${player}`;
}
// 2
let oddAverage = 0;
for (const odd of Object.values(game.odds)) {
oddAverage += odd;
}
console.log(oddAverage / oddAverage.lenght);
// 3
for (const [team, odd] of Object.entries(game.odds)) {
const teamStr = team === "x" ? "draw" : `victory ${game[team]}`;
console.log(`Odd of ${teamStr} ${odd}`);
}
// Coding Challenge #3
const gameEvents = new Map([
[17, "GOAL"],
[36, "Substitution"],
[47, "GOAL"],
[61, "Substitution"],
[64, "Yellow card"],
[69, "Red card"],
[70, "Substitution"],
[72, "Substitution"],
[76, "GOAL"],
[80, "GOAL"],
[92, "Yellow card"],
]);
// 1
const events = [...new Set([gameEvents.values()])];
console.log(events);
// 2
gameEvents.delete(64);
console.log(gameEvents);
// 3
console.log(
`An event happened, on average, every ${90 / gameEvents.size} minutes`
);
// 4
for (const [minute, event] of gameEvents.entries()) {
if (minute <= 45) {
console.log(`[FIRST HALF] ${minute}: ${event}`);
} else {
console.log(`[SECOND HALF] ${minute}: ${event}`);
}
}
// best way
for (const [min, event] of gameEvents) {
const half = min <= 45 ? "FIRST" : "SECOND";
console.log(`[${half} HALF] ${min}: ${event}`);
}