Changed around line 1
+ const words = [
+ "apple", "banana", "computer", "elephant", "guitar",
+ "happiness", "jungle", "kangaroo", "library", "mountain"
+ ];
+
+ 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 submitButton = document.getElementById('submit-button');
+ const feedbackMessage = document.getElementById('feedback-message');
+ const statsDisplay = document.getElementById('stats-display');
+
+ function getRandomWord() {
+ return words[Math.floor(Math.random() * words.length)];
+ }
+
+ function speakWord(word) {
+ const utterance = new SpeechSynthesisUtterance(word);
+ utterance.lang = 'en-US';
+ speechSynthesis.speak(utterance);
+ }
+
+ function updateStats() {
+ const percentage = totalAttempts > 0 ? ((correctCount / totalAttempts) * 100).toFixed(2) : 0;
+ statsDisplay.textContent = `Correct: ${correctCount} | Attempts: ${totalAttempts} | Success: ${percentage}%`;
+ }
+
+ function checkSpelling() {
+ const userInput = wordInput.value.toLowerCase();
+ totalAttempts++;
+
+ if (userInput === currentWord) {
+ feedbackMessage.textContent = "Correct! Great job!";
+ feedbackMessage.style.color = "var(--success-color)";
+ correctCount++;
+ wordInput.value = "";
+ newWord();
+ } else {
+ feedbackMessage.textContent = "Incorrect. Try again!";
+ feedbackMessage.style.color = "var(--error-color)";
+ wordInput.focus();
+ }
+
+ updateStats();
+ }
+
+ function newWord() {
+ currentWord = getRandomWord();
+ speakWord(currentWord);
+ feedbackMessage.textContent = "";
+ }
+
+ function highlightIncorrectLetters() {
+ const userInput = wordInput.value.toLowerCase();
+ let highlightedWord = "";
+
+ for (let i = 0; i < userInput.length; i++) {
+ if (userInput[i] !== currentWord[i]) {
+ highlightedWord += `${userInput[i]}`;
+ } else {
+ highlightedWord += userInput[i];
+ }
+ }
+
+ wordInput.innerHTML = highlightedWord;
+ }
+
+ // Event Listeners
+ playButton.addEventListener('click', () => {
+ newWord();
+ });
+
+ hintButton.addEventListener('click', () => {
+ highlightIncorrectLetters();
+ });
+
+ submitButton.addEventListener('click', checkSpelling);
+
+ wordInput.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') {
+ checkSpelling();
+ }
+ });
+
+ // Initialize
+ newWord();
+ updateStats();