-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
330 lines (301 loc) · 13.2 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
const sentences = [
"the sun sets here", // 15 characters
"a blue sky at night", // 15 characters
"rain falls so hard", // 15 characters
"stars shine so bright", // 15 characters
"birds fly in sky", // 15 characters
"wind blows so fast", // 15 characters
"snow falls down fast", // 15 characters
"trees grow so tall", // 15 characters
"clouds drift by slow", // 15 characters
"lights are bright", // 15 characters
"fire burns so hot", // 15 characters
"cows eat the grass", // 15 characters
"cats sleep right now", // 15 characters
"dogs bark so loud", // 15 characters
"frogs leap so high", // 15 characters
"hills are so green", // 15 characters
"cars drive so fast", // 15 characters
"bikes are so cool", // 15 characters
"trains roll on fast", // 15 characters
"ships sail so far", // 15 characters
"sunsets glow red", // 15 characters
"bees fly in air", // 15 characters
"books are read fast", // 15 characters
"rainbows shine bright", // 15 characters
"planes fly so high", // 15 characters
"clouds form so slow", // 15 characters
"i like pizza now", // 15 characters
"you play games here", // 15 characters
"we play fair now", // 15 characters
"they run so fast", // 15 characters
"i eat apples fast", // 15 characters
"you talk so loud", // 15 characters
"she runs so far", // 15 characters
"he reads books fast", // 15 characters
"they sit here now", // 15 characters
"we study so hard", // 15 characters
"i dance so well", // 15 characters
"she loves it now", // 15 characters
"he plays the drums", // 15 characters
"you paint the art", // 15 characters
"i climb the hills", // 15 characters
"the moon shines bright",// 15 characters
"we drink the milk", // 15 characters
"you laugh so loud", // 15 characters
"she speaks so soft", // 15 characters
"we watch the stars", // 15 characters
"i love my dogs now", // 15 characters
"you like the cake", // 15 characters
"we ride our bikes", // 15 characters
"i make some calls", // 15 characters
"you jump so high", // 15 characters
"i run very fast", // 15 characters
"she loves my cats", // 15 characters
"they play so well", // 15 characters
"we play some chess", // 15 characters
"she eats real fast", // 15 characters
"i build some things", // 15 characters
"he walks so slow", // 15 characters
"we sing so loud", // 15 characters
"they sleep right now", // 15 characters
"i buy new clothes", // 15 characters
"you walk here now", // 15 characters
"he runs so fast", // 15 characters
"i draw some art", // 15 characters
"they speak so well", // 15 characters
"you write some books", // 15 characters
"i send some emails", // 15 characters
"we build new walls", // 15 characters
"they laugh so loud", // 15 characters
"she paints right here", // 15 characters
"he plays new games", // 15 characters
"i read some books", // 15 characters
"you write right now", // 15 characters
"she bakes some pies", // 15 characters
"they fly so high", // 15 characters
"we talk so loud", // 15 characters
"i love all music", // 15 characters
"you watch tv now", // 15 characters
"i tell some jokes", // 15 characters
"we play right here", // 15 characters
"they sing new songs", // 15 characters
"you play some cards", // 15 characters
"she makes new pies", // 15 characters
"we study some math", // 15 characters
"i catch some fish", // 15 characters
"they talk so loud", // 15 characters
"we dance here now", // 15 characters
"i make some jokes", // 15 characters
"you catch some fish", // 15 characters
"i read some poems", // 15 characters
"he eats so fast", // 15 characters
"they sing new songs", // 15 characters
"she loves my books", // 15 characters
"we go hiking now", // 15 characters
"i run very fast", // 15 characters
"you sit here now", // 15 characters
"he works so hard", // 15 characters
"i ride my bikes", // 15 characters
"you talk here now", // 15 characters
"they laugh so loud", // 15 characters
"we make some art", // 15 characters
"i study so well", // 15 characters
"you write here now", // 15 characters
"she walks so slow", // 15 characters
"they play so well", // 15 characters
"we fly new planes", // 15 characters
"he draws so well", // 15 characters
"i work so hard", // 15 characters
"you sing new songs", // 15 characters
"i play some chess", // 15 characters
"we talk so soft", // 15 characters
"they read new books", // 15 characters
"he makes new jokes", // 15 characters
"she tells some jokes", // 15 characters
"i play new music", // 15 characters
"we laugh so loud", // 15 characters
"you build new walls", // 15 characters
"he loves my cats", // 15 characters
"i tell some stories", // 15 characters
"they fly so high", // 15 characters
"she plays so well", // 15 characters
"we read new books", // 15 characters
"he runs so slow", // 15 characters
"you make some pies", // 15 characters
"we build new cars", // 15 characters
"i play the drums", // 15 characters
"she writes here now", // 15 characters
"he reads here now", // 15 characters
"we talk so soft", // 15 characters
"you sing so loud", // 15 characters
"i walk so fast", // 15 characters
"she runs so fast", // 15 characters
"they sing so loud", // 15 characters
"we play so loud", // 15 characters
"i jump so high", // 15 characters
"you jump here now", // 15 characters
"she walks so far", // 15 characters
"i play so well", // 15 characters
"we draw so well", // 15 characters
"they ride new bikes", // 15 characters
"she makes new art", // 15 characters
"i sing new songs", // 15 characters
"we ride new horses", // 15 characters
"they jump here now", // 15 characters
"you talk so soft", // 15 characters
"i make some pies", // 15 characters
"we run so fast", // 15 characters
"she loves my dogs", // 15 characters
"they play so hard", // 15 characters
"i read some poems", // 15 characters
"you make new jokes", // 15 characters
"we sing new songs", // 15 characters
"she loves new music", // 15 characters
"i run so slow", // 15 characters
"we ride new horses", // 15 characters
"you play so fair", // 15 characters
"i read new books" // 15 characters
]
let startTime;
let timerInterval;
let currentMapping = generateRandomMapping();
// Function to get a random sentence
function getRandomSentence() {
const index = Math.floor(Math.random() * sentences.length);
return sentences[index];
}
// Function to normalize input (removes extra spaces and allows slight tolerance)
function normalizeInput(input) {
return input.trim().replace(/\s+/g, ' ').toLowerCase(); // Remove extra spaces and convert to lowercase
}
// Function to check if the challenge is completed with case slack
function checkCompletion() {
const typingArea = document.getElementById("typingArea");
const challengeSentence = document.getElementById("sentenceText").textContent.trim();
// Normalize both the user input and the challenge sentence
const userInput = typingArea.value.trim();
// Check if they match in terms of characters and case type
if (isValidMatch(userInput, challengeSentence)) {
clearInterval(timerInterval); // Stop the timer
showCompletionMessage();
} else {
showError(); // Show error message if the input is incorrect
}
}
// Function to check if input matches the challenge sentence considering the case type
function isValidMatch(userInput, challengeSentence) {
if (userInput.length !== challengeSentence.length) return false;
for (let i = 0; i < userInput.length; i++) {
const userChar = userInput[i];
const challengeChar = challengeSentence[i];
// Check if the letters match but ignore case differences
if (userChar.toLowerCase() !== challengeChar.toLowerCase()) {
return false;
}
// Ensure that the letter's case is correct (match uppercase with uppercase, lowercase with lowercase)
if ((userChar === userChar.toUpperCase() && challengeChar !== challengeChar.toUpperCase()) ||
(userChar === userChar.toLowerCase() && challengeChar !== challengeChar.toLowerCase())) {
return false;
}
}
return true; // If all checks pass, the input is correct
}
// Display the success message and time taken
function showCompletionMessage() {
const successMessage = document.getElementById("successMessage");
successMessage.style.display = 'block'; // Show the success message
document.getElementById("completionTime").textContent = `You completed it in ${getElapsedTime()} seconds!`;
}
// Display the error message when the input is incorrect
function showError() {
const errorMessage = document.getElementById("errorMessage");
errorMessage.style.display = 'block'; // Make the error message visible
errorMessage.textContent = "That's wrong! Try again!"; // Change error message text
// Optionally, hide it after a few seconds (e.g., 3 seconds)
setTimeout(() => {
errorMessage.style.display = 'none'; // Hide error message after 3 seconds
}, 3000);
}
// Reset the challenge and clear the text areas
function resetChallenge() {
document.getElementById("typingArea").value = '';
document.getElementById("notesArea").value = generateAlphabetSequence();
document.getElementById("sentenceText").textContent = getRandomSentence();
document.getElementById("successMessage").style.display = 'none'; // Hide success message on reset
document.getElementById("timerDisplay").textContent = "Time: 0s";
clearInterval(timerInterval);
startTime = undefined;
document.getElementById("errorMessage").style.display = 'none'; // Hide error message on reset
}
// Generate the alphabet sequence with arrows
function generateAlphabetSequence() {
let alphabetText = '';
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for (let i = 0; i < alphabet.length; i++) {
alphabetText += alphabet[i] + ' ->\n';
}
return alphabetText;
}
// Get elapsed time in seconds
function getElapsedTime() {
const endTime = new Date();
return Math.round((endTime - startTime) / 1000);
}
// Reset Button functionality
document.getElementById("resetButton").addEventListener("click", resetChallenge);
// Submit Button functionality
document.getElementById("submitButton").addEventListener("click", checkCompletion);
// Play Again functionality
document.getElementById("playAgainButton").addEventListener("click", resetChallenge);
// Randomly map letters for typing area
function generateRandomMapping() {
const letters = 'abcdefghijklmnopqrstuvwxyz'.split('');
const randomized = [...letters].sort(() => Math.random() - 0.5);
const mapping = {};
letters.forEach((letter, index) => {
mapping[letter] = randomized[index];
});
return mapping;
}
const typingArea = document.getElementById("typingArea");
typingArea.addEventListener("keydown", (event) => {
const char = event.key.toLowerCase();
if (currentMapping[char]) {
event.preventDefault();
typingArea.value += currentMapping[char];
}
// Start the timer on the first keystroke
if (!startTime) {
startTime = new Date();
timerInterval = setInterval(updateTimer, 1000);
}
});
// Timer update function
function updateTimer() {
document.getElementById("timerDisplay").textContent = `Time: ${getElapsedTime()}s`;
}
// Reset mapping every 24 hours
setInterval(() => {
currentMapping = generateRandomMapping();
}, 86400000);
// Display a random challenge sentence on load and reset notes area
document.getElementById("sentenceText").textContent = getRandomSentence();
document.getElementById("notesArea").value = generateAlphabetSequence();
// Dark Mode Toggle
const darkModeToggle = document.getElementById("darkModeToggle");
const body = document.body;
darkModeToggle.addEventListener("click", () => {
body.classList.toggle("dark-mode");
body.classList.toggle("light-mode");
document.getElementById("typingArea").classList.toggle("dark-mode");
document.getElementById("notesArea").classList.toggle("dark-mode");
darkModeToggle.classList.toggle("dark-mode");
darkModeToggle.classList.toggle("light-mode");
darkModeToggle.textContent = body.classList.contains("dark-mode") ? "Light Mode" : "Dark Mode";
});
// Initial Theme Set
body.classList.add("light-mode");
document.getElementById("typingArea").classList.add("light-mode");
document.getElementById("notesArea").classList.add("light-mode");
darkModeToggle.classList.add("light-mode");