-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
146 lines (112 loc) · 4.49 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* eslint-disable no-param-reassign */
// We first need to create a variable to store the random number
let secreteNumber = Math.trunc(Math.random() * 20) + 1;
// We need a score variable and convert it to a number
let score = +document.querySelector('.score').textContent;
let highscore = 0;
console.log(typeof score, secreteNumber);
// Lets add an event listener for a keypress too
document.querySelector('.guess').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
document.querySelector('.check').click();
}
});
// Helper function
const displayMessage = (message) => {
document.querySelector('.message').textContent = message;
};
// Adding an event listener to the check button
document.querySelector('.check').addEventListener('click', () => {
// Variable here to get the guess from the form input
const guess = +document.querySelector('.guess').value;
console.log(typeof guess);
// Lets check if guess number exists
/*
if (!guess) {
document.querySelector('.message').textContent = '⛔ No Number';
} else if (guess === secreteNumber) {
document.querySelector('.message').textContent = '✅ Correct Number';
document.querySelector('.number').textContent = secreteNumber;
document.querySelector('.box').style.backgroundColor = '#60b347';
// need to select all the buttons and change the border color
const allBtns = document.querySelectorAll('.btn');
// loop through each button element and apply the style
allBtns.forEach((btn) => {
const newBorderColor = '3px solid #000';
btn.style.border = newBorderColor;
});
if (score > 1) {
highscore = score;
document.querySelector('.highscore').textContent = highscore;
}
} else if (guess > secreteNumber) {
if (score > 1) {
document.querySelector('.message').textContent = '⛔ Too High';
document.querySelector('.guess').value = 'none';
score -= 1;
document.querySelector('.score').textContent = score;
} else {
document.querySelector('.message').textContent = '⛔ No More Guesses';
score = 0;
document.querySelector('.score').textContent = score;
}
} else if (guess < secreteNumber) {
if (score > 1) {
document.querySelector('.message').textContent = '⛔ Too Low';
document.querySelector('.guess').value = 'none';
score -= 1;
document.querySelector('.score').textContent = score;
} else {
document.querySelector('.message').textContent = '⛔ No More Guesses';
score = 0;
document.querySelector('.score').textContent = score;
}
}
*/
// Refactoring the above code
if (!guess) {
document.querySelector('.message').textContent = '⛔ No Number';
} else if (guess === secreteNumber) {
displayMessage('✅ Correct Number');
document.querySelector('.number').textContent = secreteNumber;
document.querySelector('.box').style.backgroundColor = '#60b347';
// Need to select all the buttons and change the border color
const allBtns = document.querySelectorAll('.btn');
// Loop through each button element and apply the style
allBtns.forEach((btn) => {
const newBorderColor = '3px solid #000';
btn.style.border = newBorderColor;
});
if (score > highscore) {
highscore = score;
document.querySelector('.highscore').textContent = highscore;
}
} else if (guess !== secreteNumber && score > 1) {
// Refactor the code using a ternary operator
guess > secreteNumber
? displayMessage('⛔ Too High')
: displayMessage('⛔ Too Low');
score--;
document.querySelector('.score').textContent = score;
}
});
// Adding an event listener to the again button
document.querySelector('.again').addEventListener('click', () => {
// Need to randomise the secreteNumber
secreteNumber = Math.trunc(Math.random() * 20) + 1;
console.log(typeof score, secreteNumber);
score = 20;
document.querySelector('.box').style.backgroundColor = '#222';
// Need to select all the buttons and change the border color
const allBtns = document.querySelectorAll('.btn');
// Loop through each button element and apply the style
allBtns.forEach((btn) => {
const newBorderColor = '3px solid limegreen';
btn.style.border = newBorderColor;
});
document.querySelector('.guess').value = '';
document.querySelector('.number').textContent = '?';
document.querySelector('.message').textContent = 'Start guessing...';
document.querySelector('.score').textContent = score;
});