-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
108 lines (81 loc) · 2.46 KB
/
app.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
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
var score = [0,1];
var team1={
name: "REAL MADRID",
runs: [],
score:0
}
var team2={
name: "HUESCA",
runs: [],
score:0
}
var toss;
window.onload = () =>{
selectToss();
updateButtonText();
updateNames();
updateScore();
}
var selectToss = () =>{
toss = Math.round(Math.random())+1;
console.log(toss);
}
var updateButtonText = () =>{
var button = document.getElementById("strike-button");
console.log(button);
var result = document.getElementById("result");
var btn1 = document.getElementById("btn1");
result.style.visibility ="";
if(team1.runs.length == 6 && team2.runs.length == 6){
button.remove();
btn1.textContent = "Try Again";
result.textContent = team1.score === team2.score ? `Its a draw`: `${team1.score > team2.score? team1.name:team2.name} Wins`;
}
else{
toss = toss === 1 ? 2 : 1;
}
button.textContent = `Strike(${toss%2 === 1 ? team1.name:team2.name} )`;
};
var updateNames = () =>{
document.getElementById("team-1-name").textContent = team1.name;
document.getElementById("team-2-name").textContent = team2.name;
}
var updateScore = () =>{
document.getElementById("team-1-score").textContent = team1.score;
document.getElementById("team-2-score").textContent = team2.score;
updateRuns();
}
var handleStrikeButtonClick = () =>{
var runs = score[Math.floor(Math.random()*score.length)];
console.log(runs);
runs = runs === 5?"W": runs;
console.log(runs);
if (toss ===1)
{
team1.runs.push(runs);
team1.score= calculateScore(team1.runs);
}
else{
team2.runs.push(runs);
team2.score = calculateScore(team2.runs);
}
updateButtonText();
updateScore();
}
var calculateScore = (runs) =>{
console.log("Calculate score method");
return runs.map(num =>{
return num =='W'? 0: num;
}).reduce((total,num) => total + num
);
};
var updateRuns = () =>{
var teamOneRunsElement = document.getElementById("team-1-round-runs").children;
var teamTwoRunsElement = document.getElementById("team-2-round-runs").children;
team1.runs.forEach((run,index)=>{
run === 1 ? teamOneRunsElement[index].style.backgroundColor = "#135813" :teamOneRunsElement[index].style.backgroundColor = "#ec524b";
});
team2.runs.forEach((run,index)=>{
run === 1 ? teamTwoRunsElement[index].style.backgroundColor = "#135813" : teamTwoRunsElement[index].style.backgroundColor = "#ec524b";
});
}