-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
74 lines (62 loc) · 2.04 KB
/
game.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
var myNumber = Math.round(Math.random() * 100);
var userNumbers = document.querySelector('.userNumbers');
var lastResult = document.querySelector('.lastResult');
var lowOrHi = document.querySelector('.lowOrHi');
var userNumSubmit = document.querySelector('.userNumSubmit');
var userNumInput = document.querySelector('.userNumInput');
var countNumbers = 1;
var resetButton;
function checkGuess() {
var userNumber = Number(userNumInput.value);
if (countNumbers === 1) {
userNumbers.textContent = 'Неправильные числа: ';
}
userNumbers.textContent += userNumber + ' ';
if (userNumber === myNumber) {
// lastResult.textContent = 'Поздравляю! Ты победил';
// lastResult.style.backgroundColor = 'green';
lowOrHi.textContent = 'Правильно!';
lowOrHi.className = 'win';
setGameOver();
} else if (countNumbers === 10) {
lowOrHi.textContent = 'Ты програл :(';
lowOrHi.className = 'lose';
setGameOver();
} else {
console.log(myNumber)
if(userNumber < myNumber) {
lowOrHi.textContent = 'Больше';
} else if(userNumber > myNumber) {
lowOrHi.textContent = 'Меньше';
}
}
countNumbers++;
userNumInput.value = '';
userNumInput.focus();
}
const logger = () => {console.log('Button clicked!')}
userNumSubmit.addEventListener('click', () => {
checkGuess()
});
// userNumSubmit.addEventListener('click', function(){
// logger()
// });
function setGameOver() {
userNumbers.textContent = '';
userNumInput.disabled = true;
userNumSubmit.disabled = true;
resetButton = document.createElement('button');
resetButton.textContent = 'Новая игра';
document.body.appendChild(resetButton);
resetButton.addEventListener('click', resetGame);
}
function resetGame () {
myNumber = Math.round(Math.random() * 100);
countNumbers = 1;
lowOrHi.textContent = '';
lowOrHi.className = 'lowOrHi'
userNumInput.value = '';
userNumInput.disabled = false;
userNumSubmit.disabled = false;
resetButton.parentNode.removeChild(resetButton);
}