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
|
// challenge 1
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,
},
};
// 分組
const [players1, players2] = game.players;
console.log(players1);
console.log(players2);
// 組裡面有兩種角色 守門員gk跟 其他球員
const [gk, ...fieldPlayers] = players1;
console.log(gk, fieldPlayers);
// 把所有隊伍人的放進去一個陣列
const allPlayers = [...players1, ...players2];
console.log(allPlayers);
// 新增三個候補
const players1Final = [...players1, "Thiago", "Coutinho", "Perisic"];
console.log(players1Final);
// 解構賦值取出變數 x的變數名改為draw
const {
odds: { team1, x: draw, team2 },
} = game;
console.log(team1, draw, team2);
// 丟一堆名字進去,使用其餘運算子合併成陣列,length計算長度
function printGoals(...players) {
console.log(`${players.length} goals were scored`);
}
printGoals("Davies", "Muller", "Lewandowski", "Kimmich");
// 勝率比大小 上面已經有寫出team1勝率跟team2勝率
team1 > team2 && console.log("Team 1 is more likely to win");
team1 < team2 && console.log("Team 2 is more likely to win");
// challenge 2
// 依序列印出game.scored array 裡面的球員 Goal 1: Lewandowski
for (const [index, item] of game.scored.entries())
console.log(`Goal ${index + 1}: ${item}`);
// 需要算出team1 x team2平均數 累加value / 長度
const odds = Object.values(game.odds); // [ 1.33, 3.25, 6.5 ]
let sum = 0;
for (const item of odds) {
sum += item;
}
console.log(sum / odds.length);
// 列印出
// Odd of victory Bayern Munich: 1.33
// Odd of draw: 3.25
// Odd of victory Borrussia Dortmund: 6.5
for (const [team, odd] of Object.entries(game.odds)) {
// Object.entries(game.odds) // [ [ 'team1', 1.33 ], [ 'x', 3.25 ], [ 'team2', 6.5 ] ]
const teamStr = team === "x" ? "draw" : `victory ${game[team]}`;
// 每次loop出來的team是字串,所以可以用這個[]方式取值
console.log(team, odd);
}
// 分數統計
// scorers = {
// Gnarby: 1,
// Hummels: 1,
// Lewandowski: 2
// }
const scorers = {};
for (const item of game.scored) {
// 屬性存在 賦值+1 不存在時賦值=1
scorers[item] ? scorers[item]++ : (scorers[item] = 1);
}
console.log(scorers);
// 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"],
]);
// 創造不重複事件陣列:迭代巢狀陣列的value變成一個陣列 不重複使用new Set(array)
const events = new Set(gameEvents.values());
console.log(events);
// 移除map中64分鐘的資料
gameEvents.delete(64);
console.log(gameEvents);
// 列印出發生事件avg時間長度"An event happened, on average, every 9 minutes" 一局90分鐘
const time = [...gameEvents.keys()].pop();
console.log(time); // 92 有超過90分鐘因此另外計算
console.log(
`An event happened, on average, every ${time / gameEvents.size} minutes`
);
// 加上前後半場標示[FIRST HALF] 17: ⚽️ GOAL
for (const [key, value] of gameEvents) {
const half = key <= 45 ? "First" : "Second";
console.log(`[${half} HALF]${key}: ${value}`);
}
|