-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataModule.js
More file actions
313 lines (256 loc) · 10.6 KB
/
dataModule.js
File metadata and controls
313 lines (256 loc) · 10.6 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
var dataModule = (function(){
var lineReturn = '|';
//shuffle function
var shuffle = function(array){
//[1, 2, 3] -> [3, 1, 2]
//newArray[]
//select random element: 2
//newArray[2]
//oldArray[1, 3]
//select random element: 1
//newArray[2, 1]
//oldArray[3]
//select random element: 3
//newArray[2, 1, 3]
//oldArray[]
var newArray = [];
var randomIndex;
var randomElement;
while(array.length > 0){
//take a random element from array and add it to newArray
randomIndex = Math.floor(Math.random() * array.length);
randomElement = array[randomIndex];
newArray.push(randomElement);
//delete randomElement from array
array.splice(randomIndex, 1);
}
return newArray;
};
//capitalize first letter of a string
String.prototype.capitalize = function(){
var newString = '';
var firstCharCap = this.charAt(0).toUpperCase();
var remainingChar = this.slice(1);
newString = firstCharCap + remainingChar;
return newString;
};
//capitalizeRandom function
//array['word1', 'word2', 'word3']
//array['Word1', 'word2', 'Word3']
var capitalizeRandom = function(arrayOfStrings){
return arrayOfStrings.map(function(currentWord){
var x = Math.floor(4 * Math.random()); //chances of x equal to 3: 25%
return (x == 3)? currentWord.capitalize() : currentWord;
})
};
//addRandomPunctuation function
//array['word1', 'word2', 'word3']
//array['word1.', 'word2?', 'word3,']
var addRandomPunctuation = function(arrayOfStrings){
return arrayOfStrings.map(function(currentWord){
var randomPunctuation;
var items = [lineReturn, '?', ',', ',', ',', ',', '.', '.', '!', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''];
var randomIndex = Math.floor(Math.random() * items.length);
randomPunctuation = items[randomIndex];
return currentWord + randomPunctuation;
});
};
//character call back used to calculate the number of correct characters inside the current word
var nbCorrectChar;
var charCallback = function(currentElement, index){
nbCorrectChar += (currentElement == this.characters.user[index])? 1 : 0;
};
var appData = {
indicators: {
testStarted: false, testEnded: false, totalTestTime: 0, timeLeft: 0
},
results: {
wpm: 0, wpmChange: 0, cpm: 0, cpmChange: 0, accuracy: 0, accuracyChange: 0 , numOfCorrectWords: 0, numOfCorrectCharacters: 0 , numOfTestCharacters: 0
},
words: {
currentWordIndex: -1, testWords: [], currentWord: {}
},
};
//word constructor
// {
// value: {correct: '', user: '' , isCorrect: false },
// characters: {correct: [], user: [], totalCorrect: 0, totalTest: 0 }
// }
var word = function(index){
//word values: correct vs user's
this.value = {
correct: appData.words.testWords[index] + ' ',
user: '',
isCorrect: false
};
//characters: correct vs user's
this.characters = {
correct: this.value.correct.split(''),
user: [],
totalCorrect: 0,
totalTest: this.value.correct.length
};
};
//update method: updates the word using the word typed by the user
word.prototype.update = function(value){
//update the user input
this.value.user = value;
//update the words status (correct or not)
this.value.isCorrect = (this.value.correct == this.value.user);
//update user characters
this.characters.user = this.value.user.split('');
//calculate the number of correct characters
//correct: ['w', 'o', 'r', 'd']
//user: ['w', 'o', 'o', 'w', 'w', 'w', 'w', 'w', 'w', 'w']
nbCorrectChar = 0;
var charCallback2 = charCallback.bind(this);
this.characters.correct.forEach(charCallback2);
this.characters.totalCorrect = nbCorrectChar;
};
return {
//indicators - test Control
//sets the total test time to x
setTestTime: function(x){
appData.indicators.totalTestTime = x;
},
//initializes time left to the total test time
initializeTimeLeft: function(){
appData.indicators.timeLeft = appData.indicators.totalTestTime;
},
//starts the test
startTest: function(){
appData.indicators.testStarted = true;
},
endTest: function(){
appData.indicators.testEnded = true;
},//ends the test
//return the remaining test time
getTimeLeft: function(){
return appData.indicators.timeLeft;
},
// reduces the time by one sec
reduceTime: function(){
appData.indicators.timeLeft --;
return appData.indicators.timeLeft;
},
//checks if there is time left to continue the test
timeLeft: function(){
return appData.indicators.timeLeft != 0;
},
//checks if the test has already ended
testEnded: function(){
return appData.indicators.testEnded;
},
//checks if the test has started
testStarted: function(){
return appData.indicators.testStarted;
},
//results
//calculates wpm and wpmChange and updates them in appData
calculateWpm: function(){
var wpmOld = appData.results.wpm;
var numOfCorrectWords = appData.results.numOfCorrectWords;
if(appData.indicators.timeLeft != appData.indicators.totalTestTime){
appData.results.wpm = Math.round(60 * numOfCorrectWords/(appData.indicators.totalTestTime - appData.indicators.timeLeft));
}else{
appData.results.wpm = 0
}
appData.results.wpmChange = appData.results.wpm - wpmOld;
return [appData.results.wpm, appData.results.wpmChange];
},
//calculates cpm and cpmChange and updates them in appData
calculateCpm: function(){
var cpmOld = appData.results.cpm;
var numOfCorrectCharacters = appData.results.numOfCorrectCharacters;
if(appData.indicators.timeLeft != appData.indicators.totalTestTime){
appData.results.cpm = Math.round(60 * numOfCorrectCharacters/(appData.indicators.totalTestTime - appData.indicators.timeLeft));
}else{
appData.results.cpm = 0
}
appData.results.cpmChange = appData.results.cpm - cpmOld;
return [appData.results.cpm, appData.results.cpmChange];
},
//calculates accuracy and accuracyChange and updates them in appData
calculateAccuracy: function(){
var accuracyOld = appData.results.accuracy;
var numOfCorrectCharacters = appData.results.numOfCorrectCharacters;
var numOfTestCharacters = appData.results.numOfTestCharacters;
if(appData.indicators.timeLeft != appData.indicators.totalTestTime){
if(numOfTestCharacters != 0){
appData.results.accuracy = Math.round(100 * numOfCorrectCharacters/numOfTestCharacters);
}else{
appData.results.accuracy = 0
}
}else{
appData.results.accuracy = 0;
}
appData.results.accuracyChange = appData.results.accuracy - accuracyOld;
return [appData.results.accuracy, appData.results.accuracyChange];
},
//test words
// fills words.testWords
fillListOfTestWords: function(textNumber, words){
var result = words.split(" ");
if(textNumber == 0){
//shuffle words
result = shuffle(result);
//capitalise random strings
result = capitalizeRandom(result);
//add a random punctuation
result = addRandomPunctuation(result);
}
appData.words.testWords = result;
},
// get list of test words: words.testWords
getListofTestWords: function(){
return appData.words.testWords;
},
// increments the currentWordIndex - updates the current word (appData.words.currentWord) by creating a new instance of the word class - updates numOfCorrectWords, numOfCorrectCharacters and numOfTestCharacters
moveToNewWord: function(){
if(appData.words.currentWordIndex > -1){
//update the number of correct words
if(appData.words.currentWord.value.isCorrect == true){
appData.results.numOfCorrectWords ++;
}
//update number of correct characters
appData.results.numOfCorrectCharacters += appData.words.currentWord.characters.totalCorrect;
//update number of test characters
appData.results.numOfTestCharacters += appData.words.currentWord.characters.totalTest;
}
appData.words.currentWordIndex ++;
var currentIndex = appData.words.currentWordIndex;
var newWord = new word(currentIndex);
appData.words.currentWord = newWord;
},
//get the current word index
getCurrentWordIndex(){
return appData.words.currentWordIndex;
},
//get current word
getCurrentWord(){
var currentWord = appData.words.currentWord;
return {
value: {
correct: currentWord.value.correct,
user: currentWord.value.user
}
};
},
// updates current word using user input
updateCurrentWord: function(value){
appData.words.currentWord.update(value);
},
getLineReturn(){
return lineReturn;
},
getCertificateData(){
return {
wpm: appData.results.wpm,
accuracy: appData.results.accuracy
};
},
returnData(){
console.log(appData);
}
}
})();