-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
195 lines (143 loc) · 5.09 KB
/
main.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
189
190
191
192
193
//TO DO
// - If the player is changed mid game, the state should be preserved
window.onload = function() {
loadPlayer()
document.getElementById("answer").focus();
document.querySelector('select').addEventListener('change', loadPlayer);
}
// Get answer field
var answer = document.getElementById("answer");
// Execute a function when the user presses a key on the keyboard
answer.addEventListener("keypress", function(e) {
// If the user presses the "Enter" key on the keyboard
console.log(e.key)
if (e.key === "Enter") {
// Cancel the default action, if needed
e.preventDefault();
// Trigger the button element with a click
checkAnswer()
}
});
function disableSubmitButton(){
document.getElementById('submitBtn').disabled = true;
}
function enableSubmitButton(){
document.getElementById('submitBtn').disabled = false;
}
function disableNextButton(){
document.getElementById('nextQuestionBtn').disabled = true;
}
function enableNextButton(){
document.getElementById('nextQuestionBtn').disabled = false;
}
function enableInput() {
document.querySelector('input').disabled = false;
}
function disableInput() {
document.querySelector('input').disabled = true;
}
function loadPlayer(){
let currentScore = document.getElementById('currentScoreValue')
let currentScoreInt = parseInt(currentScore.textContent)
let selectedValue = document.querySelector('select').value;
if (selectedValue == 'emma' && data['sam']['highscore'] < currentScoreInt){
data['sam']['highscore'] = currentScoreInt
}else if (selectedValue =='sam' && data['emma']['highscore'] < currentScoreInt){
data['emma']['highscore'] = currentScoreInt
}
clearHangman()
disableNextButton()
let highScore = document.getElementById('highScoreValue')
highScore.textContent = data[selectedValue]['highscore']
}
function increaseLevel(){
let level = document.getElementById('levelValue')
let levelInt = parseInt(level.textContent)
level.textContent = levelInt + 1
}
function updateScore(){
let currentScore = document.getElementById('currentScoreValue')
let currentScoreInt = parseInt(currentScore.textContent)
currentScore.textContent = currentScoreInt + 1;
if (parseInt(currentScore.textContent)%10 == 0){
increaseLevel()
}
}
function updateHighScore(){
let currentScore = document.getElementById('currentScoreValue')
let currentScoreInt = parseInt(currentScore.textContent)
let highScore = document.getElementById('highScoreValue')
let highScoreInt = parseInt(highScore.textContent)
let activePlayer = document.querySelector('select').value;
if (currentScoreInt > highScoreInt ){
data[activePlayer]['highscore'] = currentScoreInt;
}
}
// MODIFY THIS TO CHANGE INITIAL STATE VALUES MANUALLY
function resetGameState(){
let currentScore = document.getElementById('currentScoreValue')
currentScore.textContent = 0 // NORMALLY SET THIS TO 0
let level = document.getElementById('levelValue')
level.textContent = 1 // NORMALLY SET THIS TO 1
let activePlayer = document.querySelector('select').value;
let highScore = document.getElementById('highScoreValue')
highScore.textContent = data[activePlayer]['highscore']
}
function checkAnswer(){
let num1 = parseFloat(document.getElementById('num1').textContent)
let num2 = parseFloat(document.getElementById('num2').textContent)
let answer = parseFloat(document.getElementById('answer').value)
let failMsg = document.getElementById('msg')
let sum = num1 + num2;
if(sum==answer){
failMsg.textContent = "Correct!"
failMsg.style.color = "green"
updateScore()
disableSubmitButton()
enableNextButton()
}else{
if(wrongCounter<8){
wrongCounter++
drawHangman()
failMsg.textContent = `Keep trying! ${9-wrongCounter} turns remaining.`
}else if(wrongCounter ==8){
wrongCounter++
drawHangman()
failMsg.textContent = "Clear the hangman and try again!"
updateHighScore()
// resetGameState()
disableNextButton()
disableSubmitButton()
disableInput()
}
failMsg.style.color = "red"
}
}
function showAnswer(){
let num1 = parseFloat(document.getElementById('num1').textContent)
let num2 = parseFloat(document.getElementById('num2').textContent)
let result = document.getElementById('result')
let sum = num1 + num2
result.textContent = sum
disableInput()
disableSubmitButton()
enableNextButton()
}
function nextQuestion(){
// clearHangman()
enableSubmitButton()
disableNextButton()
enableInput()
let num1 = document.getElementById('num1')
let num2 = document.getElementById('num2')
let msg = document.getElementById('msg')
let result = document.getElementById('result')
let answer = document.getElementById('answer')
let level = parseInt(document.getElementById('levelValue').textContent)
msg.textContent = ""
result.textContent = ""
answer.value = ""
num1.textContent = Math.ceil(Math.random() * 10 * level)
num2.textContent = Math.ceil(Math.random() * 10 * level)
document.getElementById("answer").focus();
}