-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (103 loc) · 2.74 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
let scoreStr = localStorage.getItem('Score');
let score;
resetScore(scoreStr);
function resetScore(scoreStr){
score= scoreStr ? JSON.parse(scoreStr) : {
// Use of Gaurd Operator
win: 0,
lost: 0,
tie: 0,
};
score.displayScore = function(){
return `Win: ${score.win} | Lost: ${score.lost} | Tie: ${score.tie}`;
};
showResult();
};
// Get the modal and buttons
let modal = document.getElementById('infoModal');
let infoBtn = document.getElementById('info');
let closeModalBtn = document.getElementById('closeModalBtn');
// Attach event listeners
infoBtn.addEventListener('click', openModal);
closeModalBtn.addEventListener('click', closeModal);
// Function to open the modal
function openModal() {
modal.style.display = 'block';
}
// Function to close the modal
function closeModal() {
modal.style.display = 'none';
}
function generateComputerChoice(){
let choice;
let randomNumber = Math.random()*3;
if(randomNumber > 0 && randomNumber <= 1){
choice = 'Bat';
}
else if(randomNumber <= 2){
choice = 'Ball';
}
else{
choice = 'Wicket';
}
return choice;
}
function getResult(userMove, computerMove){
if(userMove === 'Bat'){
if(computerMove === 'Bat'){
score.tie++;
return `Tie 🤝`;
}
else if (computerMove === 'Ball'){
score.win++;
return 'You Won🎉';
}
else if(computerMove === 'Wicket'){
score.lost++;
return `Computer Won🖥️ `;
}
}
else if(userMove === 'Ball'){
if (computerMove === 'Wicket'){
score.win++;
return `You Won🎉`;
}
else if(computerMove === 'Ball'){
score.tie++;
return `Tie 🤝`;
}
else if(computerMove === 'Bat'){
score.lost++;
return `Computer Won🖥️ `;
}
}
else if(userMove === 'Wicket'){
if (computerMove === 'Bat'){
score.win++;
return `You Won🎉`;
}
else if(computerMove === 'Wicket'){
score.tie++;
return `Tie 🤝`;
}
else if(computerMove === 'Ball'){
score.lost++;
return `Computer Won🖥️ `;
}
}
}
function showResult(userMove, computerMove, resultMsg){
localStorage.setItem('Score', JSON.stringify(score));
document.querySelector('#user-move').innerText =
userMove !== undefined ? `👤 You have chosen a ${userMove}.` : '';
//Insted of this we can also use default operator.
// userMove ? `👤 You have chosen a ${userMove}.` : '';
document.querySelector('#computer-move').innerText =
computerMove !== undefined ? `🤖 Computer choice is a ${computerMove}.` : '';
document.querySelector('#result').innerText =
resultMsg !== undefined ? `🏆 Result: ${resultMsg}` : '';
document.querySelector('#score').innerText = `🎯 Score: ${score.displayScore()}`;
// alert(`You have chosen a ${userMove}. Computer choice is a ${computerMove}.
// And The Result is: ${resultMsg};
// ${score.displayScore()}`);
}