-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathchallenge.js
217 lines (199 loc) · 6.02 KB
/
challenge.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
/**
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.
Your tasks:
1. Create one player array for each team (variables 'players1' and
'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
3. Create an array 'allPlayers' containing all players of both teams (22
players)
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'
5. Based on the game.odds object, create one variable for each odd (called
'team1', 'draw' and '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)
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.
Test data for 6.: First, use players 'Davies', 'Muller', 'Lewandowski' and 'Kimmich'.
Then, call the function again with players from game.scored
GOOD LUCK 😀
**/
// 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,
// },
// };
// // 1.
// const [player1, player2] = game.players;
// console.log(player1, player2);
// // 2.
// const [gk, ...fieldPlayers] = player1;
// console.log(gk, fieldPlayers);
// // 3.
// const allPlayers = [...player1, ...player2];
// console.log(allPlayers);
// // 4.
// const players1Final = [...player1, "Thiago", "Coutinho", "Perisic"];
// console.log(players1Final);
// // 5.
// // const {
// // odds: { team1, x: draw, team2 },
// // } = game;
// // or
// const { team1, x: draw, team2 } = game.odds;
// console.log(team1, draw, team2);
// // 6.
// function printGoals(...players) {
// console.log(...players, `${players.length} goals scored ⚽`);
// }
// printGoals("Davies", "Muller", "Lewandowski", "Kimmich");
// printGoals(...game.scored);
// // 7.
// console.log(
// team1 < team2 && "Team 1 is more likely to win",
// team1 > team2 && "Team 2 is more likely to win"
// );
// ----------------------------------------
// Coding Challenge #2
// ----------------------------------------
/*
Let's continue with our football betting app!
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, exaclty 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 😉
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 😀
*/
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,
},
};
// 1.
console.log("----- 🔸Challenge 1🔸 -------");
for (const [goal, playerName] of Object.entries(game.scored)) {
console.log(`Goal ${Number(goal) + 1}: ${playerName}`);
}
// Jonas Solution
// for (const [i, playerName] of game.scored.entries()) {
// console.log(`Goal ${i + 1}: ${playerName}`);
// }
// 2.
console.log("----- 🔸Challenge 2🔸 -------");
let sum = 0;
for (const odds of Object.values(game.odds)) {
sum += odds;
}
const average = sum / Object.values(game.odds).length;
console.log(average);
// Jonas Solution
// const odds = Object.values(game.odds);
// let average = 0;
// for (const odd of odds average += odds;
// average /= odds.length;
// console.log(average);
// 3.
console.log("----- 🔸Challenge 3🔸 -------");
console.log(
`Odd of victory ${game.team1}: ${game.odds.team1} 🟢
Odd of draw: ${game.odds.x} 🟡
Odd of victory ${game.team2}: ${game.odds.team2} 🔴
`
);
for (const [team, odd] of Object.entries(game.odds)) {
const teamStr = team === "x" ? "draw" : `victory ${game[team]}`;
console.log(`Odd of ${teamStr} ${odd}`);
}
const scorers = {};
for (const player of game.scored) {
scorers[player] ? scorers[player]++ : (scorers[player] = 1);
}
console.log(scorers);