-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
183 lines (149 loc) · 6.87 KB
/
scripts.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
import { words } from './WORDSLIST/words.js';
const targetText = document.getElementById('targetText');
const userInput = document.getElementById('userInput');
const congratulations = document.getElementById('congratulations');
const clickInstruction = document.getElementById('clickInstruction');
const resetButton = document.getElementById('resetButton');
let originalText = 'Choose a Level to Start Playing'; // Set default text
let startTime; // Variable to store the start time
let typingComplete = false; // Track whether the user has completed typing
let levelChosen = false; // Track if a level is chosen
// Initially disable the input field
userInput.disabled = true;
// Create a WPM display element
const wpmDisplay = document.createElement('p');
wpmDisplay.setAttribute('id', 'wpmDisplay');
document.querySelector('.container').appendChild(wpmDisplay); // Append to the container
// Function to get random words from the selected level
function getRandomWords(level) {
const levelWords = words[level];
const numberOfWords = level === 'easy' ? 12 : level === 'medium' ? 12 : level === 'hard' ? 8 : 50;
// Shuffle the words array
const shuffledWords = [...levelWords].sort(() => 0.5 - Math.random());
// Get the first 'numberOfWords' from the shuffled array
return shuffledWords.slice(0, numberOfWords).join(' ');
}
// Function to update the displayed text based on user input
function updateText(inputText) {
let formattedText = '';
const currentIndex = inputText.length;
for (let i = 0; i < originalText.length; i++) {
const currentChar = originalText[i] === ' ' ? ' ' : originalText[i]; // Display spaces
const inputChar = inputText[i] === ' ' ? ' ' : inputText[i]; // Handle space input
if (i < currentIndex) {
if (originalText[i] === ' ' && inputText[i] !== ' ') {
// If the original is a space, but the input is not, mark it as incorrect
formattedText += `<span class="incorrect-space" style="background-color: rgba(255, 51, 51, 0.3);"> </span>`;
}
else if (inputChar === currentChar) {
formattedText += `<span class="correct">${currentChar}</span>`;
}
else {
formattedText += `<span class="incorrect">${currentChar}</span>`;
}
}
else if (i === currentIndex) {
formattedText += `<span class="current">${currentChar}</span>`;
}
else {
formattedText += currentChar;
}
}
targetText.innerHTML = formattedText;
}
// Function to check if the user has completed typing the text and calculate WPM
function checkCompletion(inputText) {
if (inputText === originalText) {
typingComplete = true; // Mark typing as complete
userInput.disabled = true; // Disable input field after completion
congratulations.classList.remove('hidden');
resetButton.classList.remove('hidden');
// Calculate WPM
const endTime = new Date(); // Get end time
const timeTakenInMinutes = (endTime - startTime) / 60000; // Time taken in minutes
const wordCount = originalText.split(' ').length; // Calculate the number of words
const wpm = Math.round(wordCount / timeTakenInMinutes); // Calculate WPM
// Display WPM
wpmDisplay.textContent = `Your WPM: ${wpm}`;
}
else {
congratulations.classList.add('hidden');
resetButton.classList.add('hidden');
wpmDisplay.textContent = ''; // Clear WPM display if text is not completed
}
}
// Event listener for difficulty levels
document.querySelectorAll('.level').forEach(level => {
level.addEventListener('click', (e) => {
// Remove 'data-selected' from any previously selected level
document.querySelectorAll('.level').forEach(btn => btn.removeAttribute('data-selected'));
// Mark the clicked level as selected
e.target.setAttribute('data-selected', 'true');
const selectedLevel = e.target.getAttribute('data-level');
updateLevel(selectedLevel);
userInput.disabled = false; // Enable the input field when a level is selected
userInput.focus(); // Focus on the input field
levelChosen = true; // Mark that a level has been chosen
typingComplete = false; // Reset typing completion status
});
});
// Function to update the level and text
function updateLevel(level) {
originalText = getRandomWords(level); // Get random words based on selected level
targetText.innerHTML = ''; // Clear the displayed text
userInput.value = ''; // Clear the input
congratulations.classList.add('hidden'); // Hide the congratulations message
resetButton.classList.add('hidden'); // Hide the reset button
clickInstruction.style.display = 'block'; // Show the click instruction
wpmDisplay.textContent = ''; // Clear WPM display
// Start timer
startTime = new Date(); // Record the start time
// Reset text display
targetText.innerHTML = originalText; // Set the new target text
}
// Event listener for text input
userInput.addEventListener('input', () => {
if (!typingComplete && levelChosen) { // Only allow typing if a level is chosen and typing is not complete
const inputText = userInput.value;
updateText(inputText);
checkCompletion(inputText);
}
});
// Function to focus on input when the container is clicked
function focusInput() {
if (levelChosen && !typingComplete) {
userInput.focus();
}
}
// Hide instruction when input is focused
userInput.addEventListener('focus', () => {
clickInstruction.style.display = 'none';
});
// Show instruction if the input loses focus
userInput.addEventListener('blur', () => {
if (userInput.value === '') {
clickInstruction.style.display = 'block';
}
});
// Hide instruction when the tab is focused
window.addEventListener('focus', () => {
clickInstruction.style.display = 'none';
});
// Show instruction when the tab loses focus
window.addEventListener('blur', () => {
clickInstruction.style.display = 'block';
});
// Reset button event listener
resetButton.addEventListener('click', () => {
userInput.disabled = true; // Disable input
levelChosen = false; // Reset level chosen status
typingComplete = false; // Reset typing completion status
targetText.innerHTML = 'Choose a Level to Start Playing'; // Reset text
userInput.value = ''; // Clear the input field
wpmDisplay.textContent = ''; // Clear WPM display
congratulations.classList.add('hidden'); // Hide the congratulations message
resetButton.classList.add('hidden'); // Hide the reset button
clickInstruction.style.display = 'block'; // Show the click instruction
});
// Click event to focus on the input
document.querySelector('.container').addEventListener('click', focusInput);