-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfootball.js
More file actions
141 lines (120 loc) Β· 4.36 KB
/
Copy pathfootball.js
File metadata and controls
141 lines (120 loc) Β· 4.36 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
/*
We're building a football betting app (soccer for my American friends π
)!
Suppose we get data from a web service about a certain game (below). In this challenge we're gonna work with the data. So here are 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: 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 finalPlayers1 = [...player1, 'p1', 'p2', 'p3'];
console.log(finalPlayers1);
//5
const { team1, x, team2 } = game.odds;
console.log(team1, x, team2);
//6
const printGoals = (...arr) => {
console.log(`${arr.length} no of goal is scored.`);
};
printGoals('Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels');
printGoals('Lewandowski', 'Gnarby');
printGoals(...game.scored);
//7
team1 < team2 && console.log('team1 is more likely to win');
team1 > team2 && console.log('team1 is more likely to win');
/*
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 π
*/
//1
for (const [i, player] of game.scored.entries()) {
console.log(`Goal ${i + 1}: ${player}`);
}
//2
let avg = 0,
count = 0;
for (const [i, score] of Object.values(game.odds).entries()) {
avg += score;
count++;
}
console.log(avg / count);
//3
for (const [key, value] of Object.entries(game.odds)) {
if (key === 'x') {
console.log(`Odd of draw: ${value}`);
} else console.log(`Odd of victory ${game[key]}: ${value}`);
}
//bonus
const scorers = {};
for (const item of game.scored) {
console.log(item);
scorers[item] = (scorers[item] || 0) + 1;
}
console.log(scorers);