-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
188 lines (166 loc) · 5.46 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
**************** Guess The Number Game ****************
* NOTE: console.log is used and commented for test pourpose
* DONE: Get user value from input and save it to variable numberGuess
* DONE: Generate a random number 1 to 100 and save it to variable correctNumber
* DONE: Console whether the guess is too high, too low, or is correct inside playGame function
* DONE: Create a function called displayResult to move the logic for if the guess is too high, too low, or correct
* DONE: Complete the showYouWon, showNumberAbove, showNumberBelow
* DONE: Use the showYouWon... functions within displayResult to display the correct dialog
* DONE: Save the guess history in a variable called guess
* DONE: Display the guess history using displayHistory() function
* TODO: Use the initGame() function to restart the game
*/
// Variable to store the list of guesses
let guesses = [];
// Variable for store the correct random number
let correctNumber = getRandomNumber();
// console.log(correctNumber);
window.onload = function() {
document.getElementById("number-submit").addEventListener("click", playGame);
document.getElementById("restart-game").addEventListener("click", initGame);
}
/**
* Functionality for playing the whole game
*/
function playGame(){
// *CODE GOES BELOW HERE *
let numberGuess = document.getElementById('number-guess').value;
// console.log(correctNumber);
// console.log(numberGuess);
displayResult(numberGuess);
saveGuessHistory(numberGuess);
displayHistory();
}
/**
* Show the result for if the guess it too high, too low, or correct
* HINT: Use if, else if, else statement
*/
// *CODE GOES BELOW HERE *
function displayResult(numberGuess){
if (numberGuess > correctNumber) {
showNumberAbove();
} else if (numberGuess < correctNumber) {
showNumberBelow();
} else {
showYouWon();
}
}
/**
* Initialize a new game by resetting all values and content on the page
* HINT: reset the correctNumber, guesses, and HTML content
*/
function initGame(){
// *CODE GOES BELOW HERE *
// Reset the correct number
correctNumber = getRandomNumber();
// Reset the result display
document.getElementById("result").innerHTML = "";
// Reset the guesses array
guesses = [];
// Reset the guesses history display
displayHistory();
}
/**
* Reset the HTML content for guess history
*/
function resetResultContent(){
document.getElementById("result").innerHTML = "";
}
/**
* Return a random number between 1 and 100
* HINT: Use Math.random
*/
function getRandomNumber(){
// *CODE GOES BELOW HERE *
// Math.random gives us the random numbr
// NOTE: that is random function is inclusive of 0 and exclusive of 1
// that means that it can go till 999.....
let randomNumber = Math.random();
// Math.floor makes decimal to integer
// Here we multiply by 100 to make it to 2 digit number
// We add 1 beacuase of the random number method in exclusive of 1
let wholeNumber = Math.floor(randomNumber * 100) + 1;
// console.log(randomNumber);
// console.log(wholeNumber);
return wholeNumber;
}
/**
* Save guess history
* HINT: Search Google "append to array in javascript"
* HINT: Use the guesses variable
*/
function saveGuessHistory(guess) {
// *CODE GOES BELOW HERE *
guesses.push(guess);
console.log(guesses);
}
/**
* Display guess history to user
* HTML TO USE:
* <ul class='list-group'>
* <li class='list-group-item'>You guessed {number}</li
* </ul>
* HINT: use while loop and string concatentation to create a list of guesses
*/
function displayHistory() {
let index = guesses.length -1; // TODO
let list = "<ul class='list-group'>";
// *CODE GOES BELOW HERE *
while (index >= 0){
list += "<li class='list-group-item'>" + "You guessed " + guesses[index] + "</li>";
index--;
}
list += '</ul>'
document.getElementById("history").innerHTML = list;
}
/**
* Retrieve the dialog based on if the guess is wrong or correct
*/
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!"
/**
* Retrieve the dialog using the getDialog() function
* and save it to variable called dialog
* HINT: Use the 'won' and text parameters
*/
// *CODE GOES BELOW HERE *
let dialog = getDialog('won', text);
document.getElementById("result").innerHTML = dialog;
}
function showNumberAbove(){
const text = "Your guess is too high!"
/**
* Retrieve the dialog using the getDialog() function
* and save it to variable called dialog
* HINT: Use the 'warning' and text parameters
*/
// *CODE GOES BELOW HERE *
let dialog = getDialog('warning', text);
document.getElementById("result").innerHTML = dialog;
}
function showNumberBelow(){
const text = "Your guess is too low!"
/**
* Retrieve the dialog using the getDialog() function
* and save it to variable called dialog
* HINT: Use the 'warning' and text parameters
*/
// *CODE GOES BELOW HERE *
let dialog = getDialog('warning', text);
document.getElementById("result").innerHTML = dialog;
}