Skip to content
This repository was archived by the owner on Oct 1, 2025. It is now read-only.

Commit d057887

Browse files
committed
feat: Coding Challenge 2 of DS and Modern Operators Done
1 parent a9153fc commit d057887

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed
Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Coding Challenge 1
2+
13
/*
24
We're building a football betting app (soccer for my American friends 😅)!;;
35
Suppose we get data from a web service about a certain game('game' variable on
@@ -103,4 +105,62 @@ const drawLikely = (draw >= team1 && draw >= team2);
103105

104106
drawLikely && console.log('Draw is likely');
105107
(!drawLikely && team1 > team2) && console.log('Team 1 is likely to win');
106-
(!drawLikely && team2 > team1) && console.log('Team 2 is likely to win');
108+
(!drawLikely && team2 > team1) && console.log('Team 2 is likely to win');
109+
110+
// Coding Challenge 2
111+
112+
/*
113+
1. Loop over the game.scored array and print each player name to the console,
114+
along with the goal number(Example: "Goal 1: Lewandowski");
115+
2. Use a loop to calculate the average odd and log it to the console(We already
116+
studied how to calculate averages, you can go check if you don't remember);
117+
3. Print the 3 odds to the console, but in a nice formatted way, exactly like this:
118+
Odd of victory Bayern Munich: 1.33
119+
Odd of draw: 3.25
120+
Odd of victory Borrussia Dortmund: 6.5
121+
Get the team names directly from the game object, don't hardcode them
122+
(except for "draw").Hint: Note how the odds and the game objects have the
123+
same property names 😉
124+
4. Bonus: Create an object called 'scorers' which contains the names of the
125+
players who scored as properties, and the number of goals as the value.In this;
126+
game, it will look like this:
127+
128+
{
129+
Gnarby: 1,
130+
Hummels: 1,
131+
Lewandowski: 2;
132+
}
133+
*/
134+
135+
const scorers = {};
136+
for (let [index, player] of game.scored.entries())
137+
{
138+
console.log(`\nGoal ${ index + 1 }: ${ player }`);
139+
140+
if (scorers?.[player])
141+
scorers[player]++;
142+
else
143+
scorers[player] = 1;
144+
}
145+
146+
let odds = Object.values(game.odds);
147+
let avr = 0;
148+
149+
for (let odd of odds)
150+
avr += odd;
151+
avr /= odds.length;
152+
153+
console.log(`Odds Average: ${ avr }`);
154+
155+
odds = Object.entries(game.odds);
156+
157+
for (let [key, value] of odds)
158+
if (game?.[key])
159+
console.log(`Odd of victory ${ game[key] }: ${ value }`);
160+
else
161+
console.log(`Odd of draw: ${ value }`);
162+
163+
let scorersMsg = 'Scorers';
164+
for (let [key, value] of Object.entries(scorers))
165+
scorersMsg = scorersMsg.concat(`\n${ key }: ${ value }`);
166+
console.log(scorersMsg);

0 commit comments

Comments
 (0)