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 correctCountSpan = document.getElementById('correct-count');
+ const totalCountSpan = document.getElementById('total-count');
+ const successRateSpan = document.getElementById('success-rate');
+
+ const playButton = document.querySelector('.key.play');
+ const hintButton = document.querySelector('.key.hint');
+ const enterButton = document.querySelector('.key.enter');
+ const keys = document.querySelectorAll('.key:not(.play):not(.hint):not(.enter)');
+
+ async function fetchWords() {
+ const response = await fetch(wordListUrl);
+ const text = await response.text();
+ words = text.split('\n').filter(word => word.length > 3 && word.length < 10);
+ getNewWord();
+ }
+
+ function getNewWord() {
+ const randomIndex = Math.floor(Math.random() * words.length);
+ currentWord = words[randomIndex];
+ words.splice(randomIndex, 1);
+ wordInput.value = '';
+ wordInput.classList.remove('correct', 'incorrect');
+ }
+
+ function updateStats(isCorrect) {
+ totalCount++;
+ if (isCorrect) correctCount++;
+ correctCountSpan.textContent = correctCount;
+ totalCountSpan.textContent = totalCount;
+ successRateSpan.textContent = `${((correctCount / totalCount) * 100).toFixed(2)}%`;
+ }
+
+ function playWord() {
+ const utterance = new SpeechSynthesisUtterance(currentWord);
+ speechSynthesis.speak(utterance);
+ }
+
+ function checkSpelling() {
+ const userInput = wordInput.value.toLowerCase();
+ if (userInput === currentWord) {
+ wordInput.classList.add('correct');
+ updateStats(true);
+ new Audio('correct.mp3').play();
+ getNewWord();
+ } else {
+ wordInput.classList.add('incorrect');
+ updateStats(false);
+ new Audio('incorrect.mp3').play();
+ }
+ }
+
+ function highlightIncorrectLetters() {
+ const userInput = wordInput.value.toLowerCase();
+ let highlightedWord = '';
+ for (let i = 0; i < currentWord.length; i++) {
+ if (userInput[i] === currentWord[i]) {
+ highlightedWord += userInput[i];
+ } else {
+ highlightedWord += `${userInput[i] || '_'}`;
+ }
+ }
+ wordInput.innerHTML = highlightedWord;
+ }
+
+ keys.forEach(key => {
+ key.addEventListener('click', () => {
+ if (key.textContent === '←') {
+ wordInput.value = wordInput.value.slice(0, -1);
+ } else {
+ wordInput.value += key.textContent.toLowerCase();
+ }
+ });
+ });
+
+ playButton.addEventListener('click', playWord);
+ hintButton.addEventListener('click', highlightIncorrectLetters);
+ enterButton.addEventListener('click', checkSpelling);
+
+ document.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') checkSpelling();
+ });
+
+ fetchWords();