-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
74 lines (57 loc) · 1.88 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
'use strict';
const message = document.querySelector('.message');
const number = document.querySelector('.number');
const score = document.querySelector('.score');
const check = document.querySelector('.btn.check');
const highscore = document.querySelector('.highscore');
const body = document.querySelector('body');
const again = document.querySelector('.btn.again');
const guessInput = document.querySelector('.guess');
let secretNumber = Math.trunc(Math.random() * 20) + 1;
// console.log(String(secretNumber));
let scoreNumber = 20;
let highscoreNumber = 0;
document.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
checkNumber();
}
});
check.addEventListener('click', checkNumber);
function checkNumber() {
let guess = Number(guessInput.value);
/* Kiedy nie ma żadnego input*/
if (!guess) {
message.textContent = 'No number!';
// Kiedy gracz wygrywa
} else if (guess === secretNumber) {
message.textContent = `Correct number!`;
body.style.backgroundColor = `#60b347`;
number.style.width = `30rem`;
number.textContent = String(secretNumber);
if (scoreNumber > highscoreNumber) {
highscoreNumber = scoreNumber;
highscore.textContent = highscoreNumber;
}
// Kiedy liczba jest inna
} else if (guess !== secretNumber) {
if (scoreNumber > 1) {
message.textContent = guess > secretNumber ? `Too high` : `Too low`;
scoreNumber--;
score.textContent = String(scoreNumber);
} else {
message.textContent = `You lost the game!`;
score.textContent = '0';
}
}
}
//Przycisk resetu
again.addEventListener('click', () => {
number.style.width = `15rem`;
message.textContent = `Start guessing...`;
number.textContent = `?`;
score.textContent = `20`;
body.style.backgroundColor = `#222`;
guessInput.value = ``;
secretNumber = Math.trunc(Math.random() * 20) + 1;
scoreNumber = 20;
});