// Set the words to be used in the test const words = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon", "mango", "nectarine", "orange", "pear", "quince", "raspberry", "strawberry", "tangerine", "watermelon"]; // Select the elements to be used in the test const startButton = document.getElementById("start-button"); const wordContainer = document.getElementById("word-container"); const inputField = document.getElementById("input-field"); const resultsContainer = document.getElementById("results-container"); // Set the initial state of the test let currentWord; let wordsTyped = 0; let correctWords = 0; let startTime; let endTime; // Function to start the test function startTest() { // Reset the state of the test currentWord = 0; wordsTyped = 0; correctWords = 0; inputField.value = ""; resultsContainer.innerHTML = ""; wordContainer.innerHTML = ""; // Generate a random set of words const randomWords = []; for (let i = 0; i < 10; i++) { const index = Math.floor(Math.random() * words.length); randomWords.push(words[index]); } // Display the words on the screen randomWords.forEach(word => { const wordElement = document.createElement("span"); wordElement.textContent = word + " "; wordContainer.appendChild(wordElement); }); // Set the focus to the input field and start the timer inputField.focus(); startTime = Date.now(); } // Function to check the user's input function checkInput() { // Get the user's input and the current word const input = inputField.value.trim(); const word = wordContainer.children[currentWord]; // Check if the input matches the current word if (input === word.textContent.trim()) { word.classList.add("correct"); correctWords++; } else { word.classList.add("incorrect"); } // Move on to the next word currentWord++; wordsTyped++; // Check if the test is complete if (currentWord === wordContainer.children.length) { // Calculate the time and words per minute endTime = Date.now(); const timeTaken = (endTime - startTime) / 1000; const wpm = Math.round((wordsTyped / timeTaken) * 60); // Display the results resultsContainer.innerHTML = `You typed ${correctWords} out of ${wordsTyped} words correctly in ${timeTaken} seconds. Your typing speed is ${wpm} words per minute.`; } } // Add event listeners to the start button and input field startButton.addEventListener("click", startTest); inputField.addEventListener("input", checkInput);