Spaces:
Running
Running
fix countdown time, everytime I choose a specipic time like 1 minute or 5 minutes, the countdown also changes base on the specific time I chose
Browse files
script.js
CHANGED
|
@@ -161,9 +161,10 @@ let totalChars = 0;
|
|
| 161 |
function initGame() {
|
| 162 |
// Get selected time
|
| 163 |
const timeSelect = document.getElementById('time-select');
|
| 164 |
-
|
|
|
|
| 165 |
timerElement.textContent = `${timeLeft}s`;
|
| 166 |
-
|
| 167 |
const languageSelect = document.getElementById('language-select');
|
| 168 |
const selectedLanguage = languageSelect.value;
|
| 169 |
|
|
@@ -185,12 +186,11 @@ totalChars = randomSnippet.length;
|
|
| 185 |
|
| 186 |
// Highlight the first character
|
| 187 |
codeDisplay.children[0].classList.add('current');
|
| 188 |
-
|
| 189 |
// Reset game state
|
| 190 |
inputField.value = '';
|
| 191 |
inputField.disabled = false;
|
| 192 |
inputField.focus();
|
| 193 |
-
timeLeft =
|
| 194 |
timerElement.textContent = `${timeLeft}s`;
|
| 195 |
correctChars = 0;
|
| 196 |
isPlaying = false;
|
|
@@ -273,18 +273,19 @@ function endGame(isCompleted = false) {
|
|
| 273 |
endTime = new Date();
|
| 274 |
|
| 275 |
// Calculate results
|
| 276 |
-
const timeTaken = Math.max(1,
|
| 277 |
-
|
| 278 |
const wpm = Math.round((wordsTyped / timeTaken) * 60);
|
| 279 |
const accuracy = Math.round((correctChars / totalChars) * 100);
|
| 280 |
|
| 281 |
// Only show results if code was completed or time ran out
|
| 282 |
if (isCompleted || timeLeft <= 0) {
|
| 283 |
-
|
|
|
|
| 284 |
finalWPM.textContent = wpm;
|
| 285 |
finalAccuracy.textContent = `${accuracy}%`;
|
| 286 |
-
finalTime.textContent = `${
|
| 287 |
-
|
| 288 |
|
| 289 |
// Save results to database
|
| 290 |
const language = document.getElementById('language-select').value;
|
|
|
|
| 161 |
function initGame() {
|
| 162 |
// Get selected time
|
| 163 |
const timeSelect = document.getElementById('time-select');
|
| 164 |
+
const selectedTime = parseInt(timeSelect.value);
|
| 165 |
+
timeLeft = selectedTime;
|
| 166 |
timerElement.textContent = `${timeLeft}s`;
|
| 167 |
+
// Get selected language
|
| 168 |
const languageSelect = document.getElementById('language-select');
|
| 169 |
const selectedLanguage = languageSelect.value;
|
| 170 |
|
|
|
|
| 186 |
|
| 187 |
// Highlight the first character
|
| 188 |
codeDisplay.children[0].classList.add('current');
|
|
|
|
| 189 |
// Reset game state
|
| 190 |
inputField.value = '';
|
| 191 |
inputField.disabled = false;
|
| 192 |
inputField.focus();
|
| 193 |
+
timeLeft = parseInt(document.getElementById('time-select').value);
|
| 194 |
timerElement.textContent = `${timeLeft}s`;
|
| 195 |
correctChars = 0;
|
| 196 |
isPlaying = false;
|
|
|
|
| 273 |
endTime = new Date();
|
| 274 |
|
| 275 |
// Calculate results
|
| 276 |
+
const timeTaken = Math.max(1, parseInt(document.getElementById('time-select').value) - timeLeft); // at least 1 second to avoid division by zero
|
| 277 |
+
const wordsTyped = correctChars / 5;
|
| 278 |
const wpm = Math.round((wordsTyped / timeTaken) * 60);
|
| 279 |
const accuracy = Math.round((correctChars / totalChars) * 100);
|
| 280 |
|
| 281 |
// Only show results if code was completed or time ran out
|
| 282 |
if (isCompleted || timeLeft <= 0) {
|
| 283 |
+
const totalTime = parseInt(document.getElementById('time-select').value);
|
| 284 |
+
// Display results
|
| 285 |
finalWPM.textContent = wpm;
|
| 286 |
finalAccuracy.textContent = `${accuracy}%`;
|
| 287 |
+
finalTime.textContent = `${totalTime - timeLeft}s`;
|
| 288 |
+
resultsSection.classList.remove('hidden');
|
| 289 |
|
| 290 |
// Save results to database
|
| 291 |
const language = document.getElementById('language-select').value;
|