Changed around line 1
+ const wordListUrl = 'https://www.mit.edu/~ecprice/wordlist.10000';
+ let words = [];
+ let currentWord = '';
+ let correctCount = 0;
+ let totalCount = 0;
+
+ const wordInput = document.getElementById('word-input');
+ const playButton = document.getElementById('play-button');
+ const hintButton = document.getElementById('hint-button');
+ const correctCountSpan = document.getElementById('correct-count');
+ const totalCountSpan = document.getElementById('total-count');
+ const successRateSpan = document.getElementById('success-rate');
+
+ async function fetchWords() {
+ const response = await fetch(wordListUrl);
+ const text = await response.text();
+ words = text.split('\n').filter(word => word.length > 3 && word.length < 8);
+ newWord();
+ }
+
+ function newWord() {
+ currentWord = words[Math.floor(Math.random() * words.length)];
+ wordInput.value = '';
+ wordInput.style.color = '#000';
+ }
+
+ function updateStats() {
+ correctCountSpan.textContent = correctCount;
+ totalCountSpan.textContent = totalCount;
+ successRateSpan.textContent = `${Math.round((correctCount / totalCount) * 100)}%`;
+ }
+
+ function playWord() {
+ const utterance = new SpeechSynthesisUtterance(currentWord);
+ speechSynthesis.speak(utterance);
+ }
+
+ function checkWord() {
+ const userWord = wordInput.value.toLowerCase();
+ if (userWord === currentWord) {
+ correctCount++;
+ totalCount++;
+ updateStats();
+ wordInput.style.color = 'green';
+ newWord();
+ playWord();
+ } else {
+ totalCount++;
+ updateStats();
+ wordInput.style.color = 'red';
+ playWord();
+ }
+ }
+
+ function highlightIncorrect() {
+ const userWord = wordInput.value.toLowerCase();
+ let incorrectIndices = [];
+ for (let i = 0; i < userWord.length; i++) {
+ if (userWord[i] !== currentWord[i]) {
+ incorrectIndices.push(i);
+ }
+ }
+ wordInput.style.color = 'red';
+ }
+
+ document.querySelectorAll('.keyboard button').forEach(button => {
+ button.addEventListener('click', () => {
+ if (button.textContent === '←') {
+ wordInput.value = wordInput.value.slice(0, -1);
+ } else if (button.textContent === 'Enter') {
+ checkWord();
+ } else {
+ wordInput.value += button.textContent.toLowerCase();
+ }
+ });
+ });
+
+ document.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ checkWord();
+ } else if (e.key === 'Backspace') {
+ wordInput.value = wordInput.value.slice(0, -1);
+ } else if (/^[a-zA-Z]$/.test(e.key)) {
+ wordInput.value += e.key.toLowerCase();
+ }
+ });
+
+ playButton.addEventListener('click', playWord);
+ hintButton.addEventListener('click', highlightIncorrect);
+
+ fetchWords();