-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
256 lines (207 loc) · 7.53 KB
/
script.js
File metadata and controls
256 lines (207 loc) · 7.53 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
// =====================
// Game data / variables
// =====================
// Array of sample sentences that appear randomly for typing practice
const sentences = [
"Practice makes you a better programmer.",
"JavaScript controls the behavior of web pages.",
"Typing quickly and accurately is a useful skill.",
"Front end development uses HTML CSS and JavaScript.",
"Keep calm and keep writing clean code."
];
// Game time settings
let timeLimit = 30; // Total time per round (in seconds)
let timeLeft = timeLimit; // Countdown timer (mutable)
let timerId = null; // Reference to setInterval, used to stop the timer later
// Game state variables
let isGameActive = false; // Tracks whether the game is currently active
let charsTyped = 0; // Characters in the current sentence
let totalCharsTyped = 0; // Characters typed across the whole game
let mistakes = 0; // Total incorrect characters in current sentence
let currentSentence = ""; // The sentence currently displayed on screen
// =====================
// DOM elements
// =====================
// Screens / sections
const startScreen = document.getElementById("start-screen");
const gameScreen = document.getElementById("game-screen");
const resultScreen = document.getElementById("result-screen");
// Buttons
const startBtn = document.getElementById("start-btn");
const cancelBtn = document.getElementById("cancel-btn");
const playAgainBtn = document.getElementById("play-again-btn");
const homeBtn = document.getElementById("home-btn");
// On-screen stats and text elements
const timeLeftSpan = document.getElementById("time-left");
const wpmSpan = document.getElementById("wpm");
const accuracySpan = document.getElementById("accuracy");
const quoteElement = document.getElementById("quote");
const inputArea = document.getElementById("input-area");
const statusText = document.getElementById("status-text");
// Final result screen elements
const finalWpmSpan = document.getElementById("final-wpm");
const finalAccuracySpan = document.getElementById("final-accuracy");
const finalCharsSpan = document.getElementById("final-chars");
// =====================
// Utility helpers
// =====================
// Switch between screens: "start", "game", or "result"
function switchScreen(name) {
startScreen.classList.remove("active");
gameScreen.classList.remove("active");
resultScreen.classList.remove("active");
if (name === "start") startScreen.classList.add("active");
if (name === "game") gameScreen.classList.add("active");
if (name === "result") resultScreen.classList.add("active");
}
// Pick a random sentence from the predefined array
function pickRandomSentence() {
const index = Math.floor(Math.random() * sentences.length);
return sentences[index];
}
// Display the current sentence character by character in separate <span> elements
// Uses a WHILE loop to satisfy lab requirement
function renderSentence(text) {
quoteElement.innerHTML = "";
let i = 0;
while (i < text.length) {
const span = document.createElement("span");
span.textContent = text[i];
quoteElement.appendChild(span);
i++;
}
}
// Nested loops example: logs each character of each word (for rubric)
function logSentenceGrid() {
const words = currentSentence.split(" ");
for (let i = 0; i < words.length; i++) {
for (let j = 0; j < words[i].length; j++) {
console.log("Word", i, "Char", j, "=", words[i][j]);
}
}
}
// =====================
// Core game functions
// =====================
// Called when user starts a new typing game
function startGame() {
// Reset all game variables
timeLeft = timeLimit;
charsTyped = 0;
totalCharsTyped = 0;
mistakes = 0;
isGameActive = true;
// Reset display
timeLeftSpan.textContent = timeLeft;
wpmSpan.textContent = 0;
accuracySpan.textContent = 0;
statusText.textContent = "Timer started. Type the sentence below!";
statusText.style.color = "#e5e7eb";
// Choose and render a random sentence
currentSentence = pickRandomSentence();
renderSentence(currentSentence);
// demonstrate nested loops once per round (console only)
logSentenceGrid();
// Reset input area
inputArea.value = "";
inputArea.disabled = false;
inputArea.focus();
// Switch to the game screen
switchScreen("game");
// Start countdown timer (updates every second)
if (timerId) clearInterval(timerId);
timerId = setInterval(updateTimer, 1000);
}
// Cancel an active game and return to start screen
function cancelGame() {
isGameActive = false;
clearInterval(timerId);
inputArea.disabled = true;
switchScreen("start");
}
// End the game when the timer runs out or player stops
function finishGame() {
isGameActive = false;
clearInterval(timerId);
inputArea.disabled = true;
// Compute accuracy and WPM results
const { wpm, accuracy } = calculateResults();
// Display final results
finalWpmSpan.textContent = wpm;
finalAccuracySpan.textContent = accuracy;
finalCharsSpan.textContent = totalCharsTyped;
// Switch to results screen
switchScreen("result");
}
// Timer logic — decreases time and ends game when 0
function updateTimer() {
if (!isGameActive) return;
timeLeft--;
timeLeftSpan.textContent = timeLeft;
if (timeLeft <= 0) {
finishGame();
}
}
// Compute WPM (words per minute) and accuracy percentage
function calculateResults() {
// One word = 5 characters (standard WPM metric)
const minutes = (timeLimit - timeLeft) / 60 || 1;
const grossWpm = Math.round((totalCharsTyped / 5) / minutes);
// Calculate percentage of correct characters
const correctChars = totalCharsTyped - mistakes;
const accuracy = totalCharsTyped === 0
? 0
: Math.round((correctChars / totalCharsTyped) * 100);
return { wpm: grossWpm, accuracy: accuracy };
}
// Handle typing input events while the game is active
function handleTyping() {
if (!isGameActive) return;
const enteredText = inputArea.value;
// characters in current sentence
charsTyped = enteredText.length;
// total characters typed in whole game
totalCharsTyped++;
const sentenceChars = quoteElement.querySelectorAll("span");
mistakes = 0;
// Compare typed characters with the correct ones and update styles
for (let i = 0; i < sentenceChars.length; i++) {
const charSpan = sentenceChars[i];
const typedChar = enteredText[i];
if (typedChar == null) {
charSpan.classList.remove("correct", "incorrect");
} else if (typedChar === charSpan.textContent) {
charSpan.classList.add("correct");
charSpan.classList.remove("incorrect");
} else {
charSpan.classList.add("incorrect");
charSpan.classList.remove("correct");
mistakes++;
}
}
// Update live WPM and accuracy values as user types
const { wpm, accuracy } = calculateResults();
wpmSpan.textContent = wpm;
accuracySpan.textContent = accuracy;
// Load a new random sentence automatically after finishing one
if (enteredText === currentSentence) {
statusText.textContent = "Great! New sentence loaded.";
statusText.style.color = "#22c55e";
// reset counters only for this sentence
charsTyped = 0;
mistakes = 0;
currentSentence = pickRandomSentence();
renderSentence(currentSentence);
inputArea.value = "";
}
}
// =====================
// Event listeners
// =====================
// Buttons
startBtn.addEventListener("click", startGame);
playAgainBtn.addEventListener("click", startGame);
homeBtn.addEventListener("click", () => switchScreen("start"));
cancelBtn.addEventListener("click", cancelGame);
// Input typing listener
inputArea.addEventListener("input", handleTyping);