Skip to content

Commit 430dba1

Browse files
committed
complete coding challenge
1 parent caef2d3 commit 430dba1

File tree

2 files changed

+139
-30
lines changed

2 files changed

+139
-30
lines changed

09-Data-Structure-Modern-Operators-and-Strings/challenge.js

+138-29
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,107 @@ Then, call the function again with players from game.scored
2828
GOOD LUCK 😀
2929
**/
3030

31+
// const game = {
32+
// team1: "Bayern Munich",
33+
// team2: "Borrussia Dortmund",
34+
// players: [
35+
// [
36+
// "Neuer",
37+
// "Pavard",
38+
// "Martinez",
39+
// "Alaba",
40+
// "Davies",
41+
// "Kimmich",
42+
// "Goretzka",
43+
// "Coman",
44+
// "Muller",
45+
// "Gnarby",
46+
// "Lewandowski",
47+
// ],
48+
// [
49+
// "Burki",
50+
// "Schulz",
51+
// "Hummels",
52+
// "Akanji",
53+
// "Hakimi",
54+
// "Weigl",
55+
// "Witsel",
56+
// "Hazard",
57+
// "Brandt",
58+
// "Sancho",
59+
// "Gotze",
60+
// ],
61+
// ],
62+
// score: "4:0",
63+
// scored: ["Lewandowski", "Gnarby", "Lewandowski", "Hummels"],
64+
// date: "Nov 9th, 2037",
65+
// odds: {
66+
// team1: 1.33,
67+
// x: 3.25,
68+
// team2: 6.5,
69+
// },
70+
// };
71+
72+
// // 1.
73+
// const [player1, player2] = game.players;
74+
// console.log(player1, player2);
75+
76+
// // 2.
77+
// const [gk, ...fieldPlayers] = player1;
78+
// console.log(gk, fieldPlayers);
79+
80+
// // 3.
81+
// const allPlayers = [...player1, ...player2];
82+
// console.log(allPlayers);
83+
84+
// // 4.
85+
// const players1Final = [...player1, "Thiago", "Coutinho", "Perisic"];
86+
// console.log(players1Final);
87+
88+
// // 5.
89+
// // const {
90+
// // odds: { team1, x: draw, team2 },
91+
// // } = game;
92+
93+
// // or
94+
// const { team1, x: draw, team2 } = game.odds;
95+
// console.log(team1, draw, team2);
96+
97+
// // 6.
98+
// function printGoals(...players) {
99+
// console.log(...players, `${players.length} goals scored ⚽`);
100+
// }
101+
// printGoals("Davies", "Muller", "Lewandowski", "Kimmich");
102+
// printGoals(...game.scored);
103+
104+
// // 7.
105+
// console.log(
106+
// team1 < team2 && "Team 1 is more likely to win",
107+
// team1 > team2 && "Team 2 is more likely to win"
108+
// );
109+
110+
// ----------------------------------------
111+
// Coding Challenge #2
112+
// ----------------------------------------
113+
114+
/*
115+
Let's continue with our football betting app!
116+
1. Loop over the game.scored array and print each player name to the console, along with the goal number (Example: "Goal 1: Lewandowski")
117+
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)
118+
3. Print the 3 odds to the console, but in a nice formatted way, exaclty like this:
119+
Odd of victory Bayern Munich: 1.33
120+
Odd of draw: 3.25
121+
Odd of victory Borrussia Dortmund: 6.5
122+
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 😉
123+
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:
124+
{
125+
Gnarby: 1,
126+
Hummels: 1,
127+
Lewandowski: 2
128+
}
129+
GOOD LUCK 😀
130+
*/
131+
31132
const game = {
32133
team1: "Bayern Munich",
33134
team2: "Borrussia Dortmund",
@@ -70,39 +171,47 @@ const game = {
70171
};
71172

72173
// 1.
73-
const [player1, player2] = game.players;
74-
console.log(player1, player2);
174+
console.log("----- 🔸Challenge 1🔸 -------");
175+
for (const [goal, playerName] of Object.entries(game.scored)) {
176+
console.log(`Goal ${Number(goal) + 1}: ${playerName}`);
177+
}
178+
// Jonas Solution
179+
// for (const [i, playerName] of game.scored.entries()) {
180+
// console.log(`Goal ${i + 1}: ${playerName}`);
181+
// }
75182

76183
// 2.
77-
const [gk, ...fieldPlayers] = player1;
78-
console.log(gk, fieldPlayers);
79-
80-
// 3.
81-
const allPlayers = [...player1, ...player2];
82-
console.log(allPlayers);
83-
84-
// 4.
85-
const players1Final = [...player1, "Thiago", "Coutinho", "Perisic"];
86-
console.log(players1Final);
184+
console.log("----- 🔸Challenge 2🔸 -------");
185+
let sum = 0;
186+
for (const odds of Object.values(game.odds)) {
187+
sum += odds;
188+
}
189+
const average = sum / Object.values(game.odds).length;
190+
console.log(average);
87191

88-
// 5.
89-
// const {
90-
// odds: { team1, x: draw, team2 },
91-
// } = game;
192+
// Jonas Solution
193+
// const odds = Object.values(game.odds);
194+
// let average = 0;
195+
// for (const odd of odds average += odds;
196+
// average /= odds.length;
197+
// console.log(average);
92198

93-
// or
94-
const { team1, x: draw, team2 } = game.odds;
95-
console.log(team1, draw, team2);
199+
// 3.
200+
console.log("----- 🔸Challenge 3🔸 -------");
201+
console.log(
202+
`Odd of victory ${game.team1}: ${game.odds.team1} 🟢
203+
Odd of draw: ${game.odds.x} 🟡
204+
Odd of victory ${game.team2}: ${game.odds.team2} 🔴
205+
`
206+
);
96207

97-
// 6.
98-
function printGoals(...players) {
99-
console.log(...players, `${players.length} goals scored ⚽`);
208+
for (const [team, odd] of Object.entries(game.odds)) {
209+
const teamStr = team === "x" ? "draw" : `victory ${game[team]}`;
210+
console.log(`Odd of ${teamStr} ${odd}`);
100211
}
101-
printGoals("Davies", "Muller", "Lewandowski", "Kimmich");
102-
printGoals(...game.scored);
103212

104-
// 7.
105-
console.log(
106-
team1 < team2 && "Team 1 is more likely to win",
107-
team1 > team2 && "Team 2 is more likely to win"
108-
);
213+
const scorers = {};
214+
for (const player of game.scored) {
215+
scorers[player] ? scorers[player]++ : (scorers[player] = 1);
216+
}
217+
console.log(scorers);

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
</head>
1111
<body>
1212
<h1>Console.log</h1>
13-
<script src="09-Data-Structure-Modern-Operators-and-Strings/script.js"></script>
13+
<script src="09-Data-Structure-Modern-Operators-and-Strings/challenge.js"></script>
1414
</body>
1515
</html>

0 commit comments

Comments
 (0)