-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.js
More file actions
238 lines (194 loc) · 6.72 KB
/
7.js
File metadata and controls
238 lines (194 loc) · 6.72 KB
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
// Data Structures, Modern Operators and Strings
// Coding Challenge #1
// We're building a football betting app (soccer for my American friends 😅)!
// Suppose we get data from a web service about a certain game ('game' variable on
// next page). In this challenge we're gonna work with that data.
const game = {
team1: "Bayern Munich",
team2: "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,
},
};
// Your tasks:
// 1. Create one player array for each team (variables 'players1' and
// 'players2')
//let player = [...game.players[0], ...game.players[1]]
//or
const [players1, players2] = game.players;
//console.log(player)
console.log(players1);
console.log(players2);
// 2. The first player in any player array is the goalkeeper and the others are field
// players. For Bayern Munich (team 1) create one variable ('gk') with the
// goalkeeper's name, and one array ('fieldPlayers') with all the remaining 10
// field players
const [gk, ...fieldPlayers] = players1;
console.log(gk);
// 3. Create an array 'allPlayers' containing all players of both teams (22
// players)
let allPlayers = [...players1, ...players2];
console.log(allPlayers);
// 4. During the game, Bayern Munich (team 1) used 3 substitute players. So create a
// new array ('players1Final') containing all the original team1 players plus
// 'Thiago', 'Coutinho' and 'Perisic'
let players1Final = [...players1, "Thiago", "Coutinho", "Perisic"];
console.log(players1Final);
// 5. Based on the game.odds object, create one variable for each odd (called
// 'team1', 'draw' and 'team2')
const { team1, x: draw, team2 } = game.odds;
console.log(team1, draw, team2);
// 6. Write a function ('printGoals') that receives an arbitrary number of player
// names (not an array) and prints each of them to the console, along with the
// number of goals that were scored in total (number of player names passed in)
const printGoals = function (arr) {
for (let element of arr) {
console.log(`${element}`);
}
console.log(`${arr.length} goals were scored`);
};
printGoals(game.scored);
// 7. The team with the lower odd is more likely to win. Print to the console which
// team is more likely to win, without using an if/else statement or the ternary
// operator.
team1 < team2 && console.log("Team 1 is more likely to win");
team1 > team2 && console.log("Team 2 is more likely to win");
// Test data for 6.: First, use players 'Davies', 'Muller', 'Lewandowski' and 'Kimmich'.
// Then, call the function again with players from game.scored
// Coding Challenge #2
// Your tasks:
// 1. Loop over the game.scored array and print each player name to the console,
// along with the goal number (Example: "Goal 1: Lewandowski")
// 2. Use a loop to calculate the average odd and log it to the console (We already
// studied how to calculate averages, you can go check if you don't remember)
// 3. Print the 3 odds to the console, but in a nice formatted way, exactly like this:
// Odd of victory Bayern Munich: 1.33
// Odd of draw: 3.25
// Odd of victory Borrussia Dortmund: 6.5
// Get the team names directly from the game object, don't hardcode them
// (except for "draw"). Hint: Note how the odds and the game objects have the
// same property names 😉
// 4. Bonus: Create an object called 'scorers' which contains the names of the
// players who scored as properties, and the number of goals as the value. In this
// game, it will look like this:
// {
// Gnarby: 1,
// Hummels: 1,
// Lewandowski: 2
// }
// GOOD LUCK 😀
//1
// let counter = 1;
// for (let element of game.scored) {
// console.log(`Goal ${counter}: ${element}`);
// counter++;
// }
// OR
for (let [i, player] of game.scored.entries())
console.log(`Goal ${i + 1}: ${player}`);
//2
let av = 0;
const values = Object.values(game.odds);
for (const odd of values) {
av += odd;
}
av /= values.length;
console.log(av);
//3
for (const [team, odd] of Object.entries(game.odds)) {
const teamName = team === "x" ? "draw" : `victory ${game[team]}`;
console.log(`Odd of ${teamName} ${odd}`);
}
//4
// 4. Bonus: Create an object called 'scorers' which contains the names of the
// players who scored as properties, and the number of goals as the value. In this
// game, it will look like this:
// {
// Gnarby: 1,
// Hummels: 1,
// Lewandowski: 2
// }
const scorers = {};
for (const player of game.scored) {
scorers[player] ? scorers[player]++ : (scorers[player] = 1);
}
console.log(scorers);
// Coding Challenge #3
// Let's continue with our football betting app! This time, we have a map called
// 'gameEvents' (see below) with a log of the events that happened during the
// game. The values are the events themselves, and the keys are the minutes in which
// each event happened (a football game has 90 minutes plus some extra time).
// Your tasks:
// 1. Create an array 'events' of the different game events that happened (no
// duplicates)
// 2. After the game has finished, is was found that the yellow card from minute 64
// was unfair. So remove this event from the game events log.
// 3. Compute and log the following string to the console: "An event happened, on
// average, every 9 minutes" (keep in mind that a game has 90 minutes)
// 4. Loop over 'gameEvents' and log each element to the console, marking
// whether it's in the first half or second half (after 45 min) of the game, like this:
// ⚽
// [FIRST HALF] 17:
// GOAL
// GOOD LUCK 😀
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 arrEvents = [...new Set(gameEvents.values())];
console.log(arrEvents);
//2
gameEvents.delete(64);
console.log(gameEvents);
//3
console.log(
`An event happened, on average, every ${90 / gameEvents.size} minutes`
);
//4
for (let [key, value] of gameEvents) {
let half = key <= 45 ? "FIRST" : "SECOND";
console.log(`[${half} HALF] ${key}: ${value}`);
}