-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
146 lines (128 loc) · 3.98 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
'use strict';
const checkBtn = document.querySelector('.check');
const againBtn = document.querySelector('.again');
let messageText = document.querySelector('.message');
const scoreText = document.querySelector('.score');
const highscoreDisplay = document.querySelector('.highscore');
let score;
let highScore = 0;
let number;
let body = document.querySelector('body');
let numberDiv = document.querySelector('.number');
let difficultyLevel;
// When the player hits the 'check' button
checkBtn.addEventListener('click', function() {
let guess = document.querySelector('.guess').value;
// When there is no number
if (!guess) {
displayMessage('⛔ No number!');
}
//When the guess is not the secret number
else if (guess != number) {
if (score > 1) {
decScore();
displayMessage(guess > number ? '📈Too high!' : '📉 Too low!');
} else {
lostGame();
}
}
// when the guess is correct
else if (number == guess) {
displayMessage('🎉 Correct number!');
disableCheckBtn();
if (highScore < score) {
highScore = score;
highscoreDisplay.textContent = score;
}
changeBCcolor('#60b347');
numberDiv.style.width = '30rem';
numberDiv.textContent = guess;
}
});
// When the player hits the 'again' button
againBtn.addEventListener('click', newNumber);
// decreasing the score
function decScore() {
score = score - 20 / difficultyLevel;
scoreText.textContent = score;
}
// When the player loses the game
function lostGame() {
displayMessage('💥 You lost the game! ');
disableCheckBtn();
scoreText.textContent = 0;
changeBCcolor('#970119');
}
// generate new number
function newNumber() {
numberDiv.textContent = '?';
numberDiv.style.width = '15rem';
document.querySelector('.guess').value = '';
checkBtn.disabled = false;
checkBtn.classList.remove('disabled');
number = Math.floor(Math.random() * 20 + 1);
score = 20;
scoreText.textContent = score;
displayMessage('Start Guessing... ');
changeBCcolor('#222');
}
function disableCheckBtn() {
checkBtn.disabled = true;
checkBtn.classList.add('disabled');
}
// displaying messages
function displayMessage(message) {
messageText.textContent = message;
}
// change the background color
function changeBCcolor(color) {
body.style.backgroundColor = color;
}
//manipulating the game difficulty
const modal = document.querySelector('.modal');
const overlay = document.querySelector('.overlay');
const btnCloseModal = document.querySelector('.close-modal');
const btnsOpenModals = document.querySelectorAll('.show-modal');
const difficultyOptions = document.querySelectorAll('.difficulty');
const startBtn = document.querySelector('.start');
for (let i = 0; i < btnsOpenModals.length; i++) {
btnsOpenModals[i].addEventListener('click', function() {
removeHidden(modal);
removeHidden(overlay);
});
}
startBtn.addEventListener('click', cooseDifficulty);
btnCloseModal.addEventListener('click', cooseDifficulty);
overlay.addEventListener('click', cooseDifficulty);
function cooseDifficulty() {
addHidden(modal);
addHidden(overlay);
for (let i = 0; i < difficultyOptions.length; i++) {
if (difficultyOptions[i].checked) {
switch (difficultyOptions[i].value) {
case 'easy':
{
difficultyLevel = 20;
break;
}
case 'medium':
{
difficultyLevel = 10;
break;
}
case 'hard':
{
difficultyLevel = 5;
break;
}
}
newNumber();
}
}
}
function removeHidden(element) {
element.classList.remove('hidden');
}
function addHidden(element) {
element.classList.add('hidden');
}