-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
213 lines (182 loc) · 6.07 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {paragraphs} from './paragraphs.js';
let typingText = document.getElementById("typing");
//Get random object containing phrase with definition from array
let getPhrase = (array) =>{
let para = array[Math.floor(Math.random()*array.length)];
return para;
}
//save chosen paragraph to variable
let phraseObj;
//set text that player will type
let gameText;
let characters = [];
// retrieving and storing paragraph array's object and their properties;
//changing the title to the word you'll learn the definition of
let gamePhrase;
let currentPhrase = document.getElementById('currentPhrase');
let focusIndex;
let focusChar;
//remove typed definition from array and replace it with a new one
function nextPhrase() {
if (paragraphs.length === 0) {
typingText.innerHTML = "Congratulations, you've completed the game!"
} else {
phraseObj = getPhrase(paragraphs);
console.log(paragraphs);
gameText = phraseObj.definition;
//split paragraph into individual letters, map it to a new arrary and give each member of the new array a span element
if(characters.length === 0){
characters = gameText.split('').map(char => {
let span = document.createElement('span');
span.innerText = char;
typingText.appendChild(span);
return span;
});
}else{
let oldText = typingText.querySelectorAll('.done');
oldText.forEach(letter =>{
letter.remove();
})
characters = /* character = an array of letters of the gameText*/ gameText.split('').map(char => {
let span = document.createElement('span');
span.innerText = char;
typingText.appendChild(span);
return span;
});
}
gamePhrase = phraseObj.phrase;
currentPhrase.style.display = 'none';
currentPhrase.innerText = gamePhrase;
currentPhrase.style.display = 'block';
//Set highlighted element
focusIndex = 0;
focusChar = characters[focusIndex];
focusChar.classList.add('focus');
typingText.style.display = 'block';
}
}
nextPhrase();
//setting up timer
let startTime = null;
let endTime = null;
let speedArray = [];
let wpm
//game stats variables
let phraseCounter = 0;
let counter = document.getElementById('counter');
let speed = document.getElementById('speed');
let tSpeed = document.getElementById('tSpeed');
let gameStats = document.getElementById('gameStats');
let speedTotal = 0;
//timer functions
function startTimer(){
if(!startTime){
startTime = Date.now();
}
}
function resetTimes(){
startTime = null;
endTime= null;
}
//time calculation
function calcTime(startT,endT){
let totalTime = endT - startT;//returns milliseconds
let seconds = totalTime/1000;
let minutes = seconds/60;
return minutes;
}
//wpm calculation
function wordsPM(totalTime){
let cpm = (gameText.length)/totalTime;
wpm = Math.floor(cpm/5);
return wpm;
}
//calculate and display average wpm
function calcAvgSpeed(wpm) {
speedArray.push(wpm);
let speedTotal = speedArray.reduce((a, b)=>{
return a + b;
})
let avgSpeed = Math.floor(speedTotal/(speedArray.length));
speed.innerText = avgSpeed;
tSpeed.innerText = speedArray[(speedArray.length -1)]
return avgSpeed;
}
//increment and display phrase counter
let gameButtons = document.getElementById('gameButtons')
function countPhrases() {
phraseCounter++;
counter.innerText = phraseCounter;
gameStats.style.display = 'block';
gameStats.style.justifySelf = 'end'
gameButtons.style.justifySelf = 'start'
}
function removeElement(element, array) {
let index = array.indexOf(element);
if (index > -1) {
array.splice(index, 1);
}
return array;
}
function game({ key }) {
//starting timer
startTimer();
//checks correct key is pressed
if (key === focusChar.innerText) {
focusChar.classList.remove('focus');
focusChar.classList.add('done');
focusChar = characters[++focusIndex];
if (focusIndex >= characters.length) {//when you get to the last letter
//set the end time
endTime = Date.now();
//calculate the total time spent typing
let minutes = calcTime(startTime, endTime);
//calculate the words per minute
let wpm = wordsPM(minutes);
//increment phrase counter and display
countPhrases();
//push wpm to speed array and
calcAvgSpeed(wpm);
//reset timers
resetTimes();
removeElement(phraseObj, paragraphs)
//sets next phrase
nextPhrase();
//clear the currently displayed wpm and display the next phrase
//clearing();
} else {
focusChar.classList.add('focus');
}
}
}
let resetBtn = document.getElementById('resetGame')
let instructionsBtn = document.getElementById('instructionsBtn')
let instructions = document.getElementById('instructions')
let instClosebtn = document.getElementById('instCloseBtn')
let creditsBtn = document.getElementById('creditsBtn')
let credits = document.getElementById('credits')
let credClosebtn = document.getElementById('credCloseBtn')
function resetGame(){
window.location.reload();
}
function showInstructions(){
instructions.style.display = 'block';
credits.style.display = 'none';
}
function closeInstructions(){
instructions.style.display = 'none';
}
function showCredits(){
credits.style.display = 'block';
instructions.style.display = 'none';
}
function closeCredits(){
credits.style.display = 'none';
}
//event Listeners
resetBtn.addEventListener('click',resetGame)
instructionsBtn.addEventListener('click',showInstructions)
instClosebtn.addEventListener('click', closeInstructions)
creditsBtn.addEventListener('click',showCredits)
credClosebtn.addEventListener('click', closeCredits)
document.addEventListener('keydown',game);