-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
79 lines (69 loc) · 2.11 KB
/
main.js
File metadata and controls
79 lines (69 loc) · 2.11 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
let computerNum = 0;
let chances = 3;
let gameOver = false;
let history = [];
let playButton = document.getElementById("play-button");
let resetButton = document.getElementById("reset-button");
let userInput = document.getElementById("user-input");
let resultArea = document.getElementById("result-area");
let gameoverArea = document.getElementById("gameover-area");
let correctArea = document.getElementById("correct-area");
let answerArea = document.getElementById("answer-area");
let chanceArea = document.getElementById("chance-area");
playButton.addEventListener("click", play);
resetButton.addEventListener("click", reset);
userInput.addEventListener("focus", function () {
userInput.value = "";
});
function pickRandomnum() {
computerNum = Math.floor(Math.random() * 100) + 1;
console.log("Answer", computerNum);
}
function play() {
let userValue = userInput.value;
if (userValue < 1 || userValue > 100) {
resultArea.textContent = "Please enter a number between 1 and 100!";
return;
}
if (history.includes(userValue)) {
resultArea.textContent = "Please enter a new number!";
return;
}
chances--;
chanceArea.textContent = `You have ${chances} chances left`;
console.log("chance", chances);
if (userValue < computerNum) {
resultArea.textContent = "Up!";
} else if (userValue > computerNum) {
resultArea.textContent = "Down!";
} else {
correctArea.textContent = "Correct!";
chanceArea.textContent = "";
gameOver = true;
}
history.push(userValue);
console.log(history);
if (chances < 1) {
gameOver = true;
}
if (gameOver == true) {
playButton.disabled = true;
gameoverArea.textContent = "GAME OVER";
answerArea.textContent = `The answer was ${computerNum}!`;
resultArea.textContent = "";
}
}
function reset() {
userInput.value = "";
pickRandomnum();
resultArea.textContent = "";
gameoverArea.textContent = "";
answerArea.textContent = "";
correctArea.textContent = "";
chances = 3;
playButton.disabled = false;
chanceArea.textContent = `You have ${chances} chances left`;
history = [];
gameOver = false;
}
pickRandomnum();