-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (76 loc) · 2.37 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
var possibleRuns = [0,1];
var team1= {
name:'Real Madrid',
runs: [],
score: 0
};
var team2= {
name:'Huesca',
runs: [],
score: 0
};
var turn;
window.onload = ()=>{
selectTurn();
updateButtonText();
updateScore();
updateNames();
}
var selectTurn = ()=>{
turn = Math.round(Math.random())+1;
console.log(turn);
}
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? `It is a draw`:`${team1.score>team2.score?team1.name:team2.name} Wins`;
}else{
turn = turn===1?2:1;
}
button.textContent = `${turn===1?team1.name:team2.name} Strike`;
}
var updateScore =()=>{
document.getElementById("team-1-score").textContent = team1.score;
document.getElementById("team-2-score").textContent = team2.score;
updateRuns();
}
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 = "#d00c0c";
});
team2.runs.forEach((run,index)=>{
run === 1 ? teamTwoRunsElement[index].style.backgroundColor = "#135813" : teamTwoRunsElement[index].style.backgroundColor = "#d00c0c";
});
}
var updateNames=()=>{
document.getElementById("team-1-name").textContent = team1.name;
document.getElementById("team-2-name").textContent = team2.name;
}
var handleStrikeButtonClick = ()=>{
var run = possibleRuns[Math.floor(Math.random()*possibleRuns.length)];
if(turn ===1){
team1.runs.push(run);
team1.score = calculateScore(team1.runs);
console.log(team1.score);
}
else{
team2.runs.push(run);
team2.score = calculateScore(team2.runs);
console.log(team2.score);
}
updateButtonText();
updateScore();
}
var calculateScore = (runs)=>{
return runs.map(run=>{
return run;
}).reduce((total,run)=>total+run,0);
};