// 编程挑战 #2
/* 让我们继续开发我们的足球投注应用程序!
原代码
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,
},
};
for (const [i, player] of game.scored.entries())
console.log(`Goal ${i + 1}: ${player}`);
let average = 0;
const odds = Object.values(game.odds);
for (const odd of odds) average += odd;
average /= odds.length;
console.log(average);
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 = {};
// 遍历数组game.scored,并统计每个球员的得分
for (let i = 0; i < game.scored.length; i++) {
const player = game.scored[i];
if (scorers[player]) {
scorers[player] += 1; // 如果球员已经存在于scorers对象中,增加得分
} else {
scorers[player] = 1; // 否则,在scorers对象中添加新球员,并设置得分为1
}
}
console.log(scorers);