Changed around line 1
+ const words = ["accommodate", "bureaucracy", "conscientious", "daiquiri", "embarrass", "fuchsia", "handkerchief", "liaison", "millennium", "occasionally"];
+ let currentWord = "";
+ let correctCount = 0;
+ let attemptedCount = 0;
+
+ const wordInput = document.getElementById('word-input');
+ const correctCountSpan = document.getElementById('correct-count');
+ const attemptedCountSpan = document.getElementById('attempted-count');
+ const successRateSpan = document.getElementById('success-rate');
+
+ function getRandomWord() {
+ if (words.length === 0) {
+ alert("Congratulations! You've spelled all the words!");
+ return;
+ }
+ const randomIndex = Math.floor(Math.random() * words.length);
+ currentWord = words.splice(randomIndex, 1)[0];
+ speakWord(currentWord);
+ }
+
+ function speakWord(word) {
+ const utterance = new SpeechSynthesisUtterance(word);
+ speechSynthesis.speak(utterance);
+ }
+
+ function updateStats() {
+ const successRate = ((correctCount / attemptedCount) * 100).toFixed(2);
+ correctCountSpan.textContent = correctCount;
+ attemptedCountSpan.textContent = attemptedCount;
+ successRateSpan.textContent = `${successRate}%`;
+ }
+
+ document.querySelectorAll('.key').forEach(button => {
+ button.addEventListener('click', () => {
+ if (button.textContent === '←') {
+ wordInput.value = wordInput.value.slice(0, -1);
+ } else if (button.textContent === 'Play') {
+ speakWord(currentWord);
+ } else if (button.textContent === 'Enter') {
+ checkSpelling();
+ } else if (button.textContent === 'Hint') {
+ highlightIncorrectLetters();
+ } else {
+ wordInput.value += button.textContent;
+ }
+ });
+ });
+
+ document.addEventListener('keydown', (event) => {
+ if (event.key === 'Backspace') {
+ wordInput.value = wordInput.value.slice(0, -1);
+ } else if (event.key === 'Enter') {
+ checkSpelling();
+ } else if (/^[a-zA-Z]$/.test(event.key)) {
+ wordInput.value += event.key.toUpperCase();
+ }
+ });
+
+ function checkSpelling() {
+ attemptedCount++;
+ if (wordInput.value.toLowerCase() === currentWord.toLowerCase()) {
+ correctCount++;
+ alert("Correct! Well done!");
+ updateStats();
+ getRandomWord();
+ wordInput.value = "";
+ } else {
+ alert("Incorrect. Try again.");
+ speakWord(currentWord);
+ updateStats();
+ }
+ }
+
+ function highlightIncorrectLetters() {
+ const inputLetters = wordInput.value.split('');
+ const wordLetters = currentWord.split('');
+ let hint = '';
+ for (let i = 0; i < wordLetters.length; i++) {
+ if (inputLetters[i] === wordLetters[i]) {
+ hint += wordLetters[i];
+ } else {
+ hint += `${wordLetters[i]}`;
+ }
+ }
+ wordInput.innerHTML = hint;
+ }
+
+ getRandomWord();