-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
144 lines (121 loc) · 3.84 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
'use strict';
// Is highscore num of attempts?
// User picks range?
const modeBtn = document.querySelector("#mode");
const modeOutput = document.querySelector(".mode");
const displayFeedback = document.querySelector(".display-feedback");
const inp = document.querySelector("#num-guess");
const numBtn = document.querySelector("#num-btn");
const mysteryNum = document.querySelector(".mystery-num");
const body = document.querySelector("body");
const score = document.querySelector("#score");
const highscore = document.querySelector("#highscore");
const guessOutput = document.querySelector(".guess-output");
let usrNum;
let highscoreVal;
let guesses = 0;
let highestScore = 0;
function genRandNum(num) {
return Math.floor(Math.random() * num) + 1;
};
// Genereta random number from the beginning
let randNum = genRandNum(20);
console.log(randNum);
// Reset
function reset(num) {
// Generate new number
randNum = genRandNum(num);
console.log(randNum);
// Reset all values
body.classList.remove("win-body");
inp.classList.remove("win-text");
mysteryNum.classList.remove("win-text");
inp.removeAttribute("disabled", "disabled");
displayFeedback.style.color = "white";
guesses = 0;
inp.value = "";
score.innerHTML = 0;
mysteryNum.innerHTML = "?";
numBtn.innerHTML = "Check!";
guessOutput.innerHTML = "Start guessing...";
displayFeedback.innerHTML = "Guess The Number!";
}
// Guess Feedback
function updateOutput(usrNum, randNum, guessOutput) {
if (usrNum < randNum) {
guessOutput.innerHTML = "Guess too low...";
} else if (usrNum > randNum) {
guessOutput.innerHTML = "Guess too high...";
} else {
guessOutput.innerHTML = "Yes!";
}
}
function correctGuess(mysteryNum, randNum, inp, displayFeedback, numBtn) {
mysteryNum.innerHTML = randNum;
body.classList.add("win-body");
inp.classList.add("win-text");
mysteryNum.classList.add("win-text");
displayFeedback.innerHTML = "Correct!";
inp.setAttribute("disabled", "disabled");
displayFeedback.style.color = "white";
numBtn.innerHTML = "Play again?";
}
// Check if answer is correct
function isCorrect(usrNum, randNum, numBtn, mysteryNum,
inp, displayFeedback, highscoreVal, guesses) {
// If user guessed right
if (usrNum === randNum && numBtn.innerHTML === "Check!") {
correctGuess(mysteryNum, randNum, inp, displayFeedback, numBtn);
if (highscoreVal > guesses) {
highscore.innerHTML = guesses;
} else if (highscoreVal === 0) {
highscore.innerHTML = guesses;
}
} else if (usrNum !== randNum) { // if user did not guess right
displayFeedback.classList.remove("incorrectDisplay");
void displayFeedback.offsetWidth;
displayFeedback.classList.add("incorrectDisplay");
displayFeedback.innerHTML = "Try again";
displayFeedback.style.color = "red";
} else if (numBtn.innerHTML === "Play again?") { //if user wants to play again
if (modeBtn.innerHTML === "Easy!") {
reset(50);
} else {
reset(20);
}
}
}
function checkAnswer() {
// Track score
guesses++;
score.innerHTML = guesses;
highscoreVal = +highscore.innerHTML;
// Get user input and highscore
usrNum = +inp.value;
// Update feedback
updateOutput(usrNum, randNum, guessOutput);
// Check if number is correct
isCorrect(usrNum, randNum, numBtn, mysteryNum,
inp, displayFeedback, highscoreVal, guesses);
}
// Test user answer on pressing enter
inp.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
checkAnswer();
}
});
// Test user answer on button click
numBtn.addEventListener("click", checkAnswer);
modeBtn.addEventListener("click", function() {
if (modeBtn.innerHTML === "Easy!") {
modeBtn.innerHTML = "Hard!";
console.log(randNum);
modeOutput.innerHTML = "50";
reset(50);
} else {
modeBtn.innerHTML = "Easy!";
console.log(randNum);
modeOutput.innerHTML = "20";
reset(20);
}
});