0% found this document useful (0 votes)
4 views

Javascript 22

Uploaded by

SOHAM PATIL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Javascript 22

Uploaded by

SOHAM PATIL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

cricket.

js

1 let ballCount = 0;
2 let currentOverScore = 0;
3 let currentScore = 0;
4 let currentOver = 1;
5 let totalScore = 0;
6 let wickets = 0;
7 let overScores = [];
8 let currentTeam = 1;
9 let teamScores = [[], []];
10 function addBall(type) {
11 if (wickets < 10 && ballCount < 6) {
12 const ballsContainer = document.getElementById("balls-container");
13 const ball = document.createElement("div");
14 ball.classList.add("ball");
15 ball.innerText = type === "dot" ? "." : type;
16 ballsContainer.appendChild(ball);
17 ballCount++;
18 if (type !== "dot") {
19 if (type !== "wicket") {
20 const runs = parseInt(type);
21 currentOverScore += runs;
22 currentScore += runs;
23 } else {
24 wickets++;
25 if (wickets === 10) {
26 endOver();
27 return;
28 }
29 }
30 }
31 if (ballCount === 6) {
32 endOver();
33 }
34 updateTeamScore();
35 displayCurrentScore();
36 }
37 }
38 function addExtraBall(type) {
39 if (wickets < 10) {
40 const ballsContainer = document.getElementById("balls-container");
41 const ball = document.createElement("div");
42 ball.classList.add("ball");
43 if (type === "wide" || type === "no") {
44 ball.innerText = type === "wide" ? "WD1" : "NB1";
45 currentOverScore += 1;
46 currentScore += 1;
47 }
48 ballsContainer.appendChild(ball);
49 updateTeamScore();
50 displayCurrentScore();
51 }
52 }
53 function displayCurrentScore() {
54 const currentScoreElement = document.getElementById("current-score");
55 currentScoreElement.innerText = `Current Score: ${currentScore} Runs and ${wickets}
56 wickets`;
57 }
58 function updateTeamScore() {
59 const team1Score = teamScores[0].reduce((total, score) => total + score, 0);
60 const team2Score = teamScores[1].reduce((total, score) => total + score, 0);
61 if (currentTeam === 2 && team2Score > team1Score) {
62 declareWinner(2, 10 - wickets, "wickets");
63 }
64 }
65 function endOver() {
66 overScores.push(currentOverScore);
67 teamScores[currentTeam - 1].push(currentOverScore);
68 totalScore += currentOverScore;
69 updateLocalStorage();
70 const cumulativeScoreDisplay = document.getElementById("cumulative-score");
71 const overScoreElement = document.createElement("div");
72 overScoreElement.innerText = `Team ${currentTeam}, Over ${currentOver}:
73 ${currentOverScore} runs`;
74 cumulativeScoreDisplay.appendChild(overScoreElement);
75 currentOver++;
76 ballCount = 0;
77 currentOverScore = 0;
78 const scoreDisplay = document.getElementById("score-display");
79 scoreDisplay.innerText = `Team ${currentTeam} Total Score: ${totalScore} runs, Wickets:
80 ${wickets}`;
81 if (currentOver > 20 || wickets === 10) {
82 endInnings();
83 } else {
84 disableButtons();
85 showNewOverButton();
86 }
87 displayCurrentScore();
88 }
89 function endInnings() {
90 if (currentTeam === 1) {
91 currentTeam = 2;
92 resetForNextTeam();
93 } else {
94 declareWinner();
95 }
96 }
97 function resetForNextTeam() {
98 overScores = [];
99 totalScore = 0;
100 wickets = 0;
101 currentOver = 1;
102 ballCount = 0;
103 currentOverScore = 0;
104 currentScore = 0;
105 const ballsContainer = document.getElementById("balls-container");
106 ballsContainer.innerHTML = "";
107 const cumulativeScoreDisplay = document.getElementById("cumulative-score");
108 cumulativeScoreDisplay.innerHTML = "";
109 const scoreDisplay = document.getElementById("score-display");
110 scoreDisplay.innerText = `Team ${currentTeam} starts their innings`;
111 const buttons = document.querySelectorAll(".buttons-container button");
112 buttons.forEach((button) => {
113 button.disabled = false;
114 });
115 }
116 function declareWinner(winningTeam, margin, unit) {
117 const scoreDisplay = document.getElementById("score-display");
118 let winnerText;
119 if (winningTeam === 1) {
120 winnerText = `Team 1 wins by ${margin} ${unit}!`;
121 } else {
122 winnerText = `Team 2 wins by ${margin} ${unit}!`;
123 }
124 scoreDisplay.innerText = `Match complete! ${winnerText}`;
125 disableButtons();
126 setTimeout(() => {
127 localStorage.clear();
128 console.log("Local storage cleared after 5 seconds.");
129 }, 5000);
130 }
131 function disableButtons() {
132 const buttons = document.querySelectorAll(".buttons-container button");
133 buttons.forEach((button) => {
134 button.disabled = true;
135 });
136 }
137 function showNewOverButton() {
138 const newOverButton = document.createElement("button");
139 newOverButton.innerText = "New Over";
140 newOverButton.onclick = resetOver;
141 document.querySelector(".buttons-container").appendChild(newOverButton);
142 }
143 function resetOver() {
144 const ballsContainer = document.getElementById("balls-container");
145 ballsContainer.innerHTML = "";
146 const buttons = document.querySelectorAll(".buttons-container button");
147 buttons.forEach((button) => {
148 button.disabled = false;
149 });
150 document
151 .querySelector(".buttons-container")
152 .removeChild(
153 document.querySelector(".buttons-container button:last-child")
154 );
155 const scoreDisplay = document.getElementById("score-display");
156 scoreDisplay.innerText = "";
157 }
158 function updateLocalStorage() {
159 localStorage.setItem("teamScores", JSON.stringify(teamScores));
160 }
161 document.addEventListener("DOMContentLoaded", () => {
162 document.getElementById("button-1").onclick = () =>
163 addBallAndCheckWinner("1");
164 document.getElementById("button-2").onclick = () =>
165 addBallAndCheckWinner("2");
166 document.getElementById("button-3").onclick = () =>
167 addBallAndCheckWinner("3");
168 document.getElementById("button-4").onclick = () =>
169 addBallAndCheckWinner("4");
170 document.getElementById("button-5").onclick = () =>
171 addBallAndCheckWinner("5");
172 document.getElementById("button-6").onclick = () =>
173 addBallAndCheckWinner("6");
174 document.getElementById("button-dot").onclick = () =>
175 addBallAndCheckWinner("dot");
176 document.getElementById("button-wicket").onclick = () =>
177 addBallAndCheckWinner("wicket");
178 document.getElementById("button-wide").onclick = () =>
179 addExtraBallAndCheckWinner("wide");
180 document.getElementById("button-no").onclick = () =>
181 addExtraBallAndCheckWinner("no");
182 });
183 function addBallAndCheckWinner(type) {
184 addBall(type);
185 checkWinner();
186 }
187 function addExtraBallAndCheckWinner(type) {
188 addExtraBall(type);
189 checkWinner();
190 }
191 function checkWinner() {
192 const team1Score = teamScores[0].reduce((total, score) => total + score, 0);
193 const team2Score = teamScores[1].reduce((total, score) => total + score, 0);
194 if (currentTeam === 2 && currentScore > team1Score) {
195 const remainingWickets = 10 - wickets;
196 declareWinner(2, remainingWickets, "wickets");
197 } else if (wickets === 10) {
198 if (currentTeam === 1) {
199 endInnings();
200 } else {
201 declareWinner(1, team1Score - currentScore, "runs");
202 }
203 }
204 }

You might also like