-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
75 lines (61 loc) · 2.51 KB
/
script.js
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
function OpeningCeremony(callbackFnc) {
setTimeout(() => {
console.log("Let the games begin");
let score = { red: 0, blue: 0, green: 0, yellow: 0 };
console.log("Initial Score: ", score);
callbackFnc(score,LongJump);
}, 1000);
}
function Race100M(score,callbackFnc) {
setTimeout(() => {
console.log("Race 100M started");
console.log("Previous score: ",score);
let redTime = Math.floor(Math.random() * 6) + 10;
let blueTime = Math.floor(Math.random() * 6) + 10;
let greenTime = Math.floor(Math.random() * 6) + 10;
let yellowTime = Math.floor(Math.random() * 6) + 10;
let times = { red: redTime, blue: blueTime, green: greenTime, yellow: yellowTime };
let sortedColors = Object.keys(times).sort((a, b) => times[a] - times[b]);
score[sortedColors[0]] += 50;
score[sortedColors[1]] += 25;
console.log("Updated Score: ", score);
console.log(`${sortedColors[0]} won the Race 100M`);
callbackFnc(score,HighJump);
}, 3000);
}
function LongJump(score,callbackFnc) {
setTimeout(() => {
console.log("Long Jump started");
console.log("Previous score: ",score);
let color = ['red', 'yellow', 'green', 'blue'][Math.floor(Math.random() * 4)];
console.log('color:', color)
score[color] += 150;
console.log("Updated Score: ", score);
console.log(`${color} won the Long Jump`);
callbackFnc(score,AwardCeremony);
}, 2000);
}
function HighJump(score,callbackFnc) {
console.log("High Jump started");
console.log("Previous score: ",score);
let userInput = prompt("What colour secured the highest jump?");
if (userInput && score.hasOwnProperty(userInput.toLowerCase())) {
score[userInput.toLowerCase()] += 100;
console.log("Updated Score: ", score);
console.log(`${userInput} won the High Jump`);
callbackFnc(score);
} else {
console.log("Event was cancelled");
callbackFnc(score);
}
}
function AwardCeremony(score) {
console.log("Award Ceremony started");
console.log('Final score:', score)
let sortedScores = Object.entries(score).sort((a, b) => b[1] - a[1]);
console.log(`${sortedScores[0][0]} came first with ${sortedScores[0][1]} points.`);
console.log(`${sortedScores[1][0]} came second with ${sortedScores[1][1]} points.`);
console.log(`${sortedScores[2][0]} came third with ${sortedScores[2][1]} points.`);
}
// Start the event
OpeningCeremony(Race100M);