-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
175 lines (154 loc) · 4.69 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
/**
* Guess The Number Game
* 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
* Done: Use the initGame() function to restart the game
*/
// Variable to store the list of guesses
// Variable for store the correct random number
let guesses = [];
let correctNumber = getRandomNumber();
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;
displayResult(numberGuess);
saveGuessHistory(numberGuess);
displayHistory(numberGuess);
}
/**
* 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 correctNumber
// Reset the result display
// Reset the guesses array
// Reset the guess history display
correctNumber = getRandomNumber();
document.getElementById("result").innerHTML= "";
guesses = [];
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 *
let randomNumber = Math.random() ;
let wholeNumber = Math.floor(randomNumber * 100) + 1;
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);
}
/**
* 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-= 1;
}
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;
}