-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
102 lines (85 loc) · 2.54 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
let guesses = [];
let correctNumber = getRandomNumber();
console.log(correctNumber);
window.onload = function () {
document.getElementById("number-submit").addEventListener("click", playGame);
document.getElementById("restart-game").addEventListener("click", initGame);
getRandomNumber();
};
function playGame() {
let numberGuess = document.getElementById("number-guess").value;
displayResult(numberGuess);
saveGuessHistory(numberGuess);
displayHistory();
}
function displayResult(numberGuess) {
if (numberGuess > correctNumber) {
showNumberAbove();
} else if (numberGuess < correctNumber) {
showNumberBelow();
} else {
showYouWon();
}
}
function initGame() {
correctNumber = getRandomNumber(); //Resets the correct number
document.getElementById("result").innerHTML = ""; //Resets the result display
guesses = []; //Resets the guesses array
displayHistory(); //Resets the guess history display
}
function resetResultContent() {
document.getElementById("result").innerHTML = "";
}
function getRandomNumber() {
let randomNumber = Math.random(); // Math.random gives number between 0,1 (1 exclusive)
let wholeNumber = Math.floor(randomNumber * 100) + 1; //Math.floor uses only the whole number part and ignores the fractional part
return wholeNumber;
}
function saveGuessHistory(guess) {
guesses.push(guess);
console.log(guesses);
}
function displayHistory() {
let index = guesses.length - 1;
let list = "<ul class='list-group'>";
while (index >= 0) {
list +=
"<li class = 'list-group-item'>" +
"You guessed " +
guesses[index] +
"</li>";
index -= 1;
}
list += "</ul>";
document.getElementById("history").innerHTML = list;
}
function getDialog(dialogType, text) {
let dialog;
switch (dialogType) {
case "warning":
dialog = "<div class='alert alert-warning' role='alert'>";
break;
case "won":
dialog = "<div class='alert alert-success' role='alert'>";
break;
}
dialog += text;
dialog += "</div>";
return dialog;
}
function showYouWon() {
const text = "Awesome job, you got it!";
let dialog = getDialog("won", text);
console.log(dialog);
document.getElementById("result").innerHTML = dialog;
}
function showNumberAbove() {
const text = "Your guess is too high!";
let dialog = getDialog("warning", text);
document.getElementById("result").innerHTML = dialog;
}
function showNumberBelow() {
const text = "Your guess is too low!";
let dialog = getDialog("warning", text);
document.getElementById("result").innerHTML = dialog;
}