Changed around line 1
+ const words = [
+ "apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew"
+ ];
+
+ let currentWord = '';
+ let correctCount = 0;
+ let totalAttempts = 0;
+
+ const playButton = document.getElementById('play-button');
+ const hintButton = document.getElementById('hint-button');
+ const wordInput = document.getElementById('word-input');
+ const enterButton = document.getElementById('enter-button');
+ const feedbackMessage = document.getElementById('feedback-message');
+ const stats = document.getElementById('stats');
+
+ function getRandomWord() {
+ return words[Math.floor(Math.random() * words.length)];
+ }
+
+ function playWord() {
+ currentWord = getRandomWord();
+ const utterance = new SpeechSynthesisUtterance(currentWord);
+ speechSynthesis.speak(utterance);
+ }
+
+ function checkSpelling() {
+ const userInput = wordInput.value.trim().toLowerCase();
+ totalAttempts++;
+ if (userInput === currentWord) {
+ correctCount++;
+ feedbackMessage.textContent = 'Correct! Well done!';
+ feedbackMessage.style.color = 'green';
+ playSound('correct');
+ } else {
+ feedbackMessage.textContent = 'Incorrect. Try again!';
+ feedbackMessage.style.color = 'red';
+ playSound('incorrect');
+ }
+ updateStats();
+ wordInput.value = '';
+ playWord();
+ }
+
+ function updateStats() {
+ const percentage = ((correctCount / totalAttempts) * 100).toFixed(2);
+ stats.textContent = `Correct: ${correctCount} | Attempts: ${totalAttempts} | Success Rate: ${percentage}%`;
+ }
+
+ function highlightIncorrectLetters() {
+ const userInput = wordInput.value.trim().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;
+ }
+
+ function playSound(type) {
+ const audio = new Audio();
+ audio.src = type === 'correct' ? 'correct.mp3' : 'incorrect.mp3';
+ audio.play();
+ }
+
+ playButton.addEventListener('click', playWord);
+ enterButton.addEventListener('click', checkSpelling);
+ hintButton.addEventListener('click', highlightIncorrectLetters);
+
+ wordInput.addEventListener('keyup', (event) => {
+ if (event.key === 'Enter') {
+ checkSpelling();
+ }
+ });
+
+ playWord();