-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
89 lines (71 loc) · 2.81 KB
/
script.js
File metadata and controls
89 lines (71 loc) · 2.81 KB
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
// Dəyişənlər
let attempts;
let maxAttempts;
let correctNumber;
let maxNumber;
let numberInp = document.getElementById("number");
let enterButton = document.getElementById("confirm");
let disappearingElements = document.getElementsByClassName("disappear");
// Köməkçi funksiyalar
function generateRandomNumber(number) {
return Math.floor(Math.random() * number) + 1;
}
function informUser(attempts, enteredNumber, correctNumber) {
let current = document.getElementById("currentResult");
let ending = `${attempts} cəhdiniz qalıb.`;
if (!(enteredNumber && correctNumber)) current.innerText = ending;
else if (attempts == 0 && enteredNumber != correctNumber) current.innerText = `Tapmadınız və uduzdunuz!\nDoğru cavab ${correctNumber} idi.`;
else if (enteredNumber < correctNumber) current.innerText = "Daha böyük ədəd daxil edin. " + ending;
else if (enteredNumber > correctNumber) current.innerText = "Daha kiçik ədəd daxil edin. " + ending;
else if (enteredNumber == correctNumber) current.innerText = `Düz tapdınız! ${maxAttempts - attempts} cəhd istifadə etdiniz.`;
}
function gameOver() {
return correctNumber == numberInp.value || attempts === 0;
}
function startGameAgain() {
disappearingElements[1].style.display = "block";
disappearingElements[2].style.display = "flex";
enterButton.removeAttribute("disabled");
numberInp.removeAttribute("disabled");
numberInp.value = "1";
}
function closeTheWindow() {
document.getElementById("farewell").style.display = "block";
setTimeout(function () { window.close(); }, 2300);
}
// Əsas funksiyalar
function startGame(button) {
let condition = document.getElementById("condition");
document.getElementById("game").style.display = "flex";
for (let element of disappearingElements) {
element.style.display = "none";
}
if (button.value == "easy") {
maxAttempts = 3;
maxNumber = 10;
}
else if (button.value == "difficult") {
maxAttempts = 5;
maxNumber = 25;
}
attempts = maxAttempts;
condition.innerText = `1-${maxNumber} aralığında ədəd daxil edin:`;
numberInp.setAttribute("max", maxNumber);
informUser(attempts, 0, 0);
correctNumber = generateRandomNumber(maxNumber);
}
function playGame() {
attempts--;
informUser(attempts, numberInp.value, correctNumber);
if (gameOver()) {
enterButton.setAttribute("disabled", "disabled");
numberInp.setAttribute("disabled", "disabled");
document.getElementById("end").style.display = "block";
}
}
function endGame(button) {
document.getElementById("game").style.display = "none";
document.getElementById("end").style.display = "none";
if (button.value == "yes") startGameAgain();
else if (button.value == "no") closeTheWindow();
}