Changed around line 1
+ class SpellIt {
+ constructor() {
+ this.words = [];
+ this.currentWord = '';
+ this.correct = 0;
+ this.total = 0;
+ this.input = document.getElementById('wordInput');
+ this.feedback = document.getElementById('feedback');
+ this.score = document.getElementById('score');
+ this.synth = window.speechSynthesis;
+
+ this.initializeEventListeners();
+ this.fetchWords();
+ }
+
+ async fetchWords() {
+ try {
+ const response = await fetch('https://www.mit.edu/~ecprice/wordlist.10000');
+ const text = await response.text();
+ this.words = text.split('\n').filter(word => word.length > 0);
+ this.getNewWord();
+ } catch (error) {
+ console.error('Error fetching words:', error);
+ }
+ }
+
+ initializeEventListeners() {
+ document.getElementById('playBtn').addEventListener('click', () => this.playWord());
+ document.getElementById('enterBtn').addEventListener('click', () => this.checkWord());
+ document.getElementById('hintBtn').addEventListener('click', () => this.showHint());
+
+ this.input.addEventListener('keypress', (e) => {
+ if (e.key === 'Enter') this.checkWord();
+ });
+
+ document.querySelectorAll('.keyboard-row button').forEach(button => {
+ button.addEventListener('click', () => {
+ this.input.value += button.textContent.toLowerCase();
+ this.input.focus();
+ });
+ });
+ }
+
+ getNewWord() {
+ const randomIndex = Math.floor(Math.random() * this.words.length);
+ this.currentWord = this.words[randomIndex];
+ this.input.value = '';
+ this.feedback.textContent = 'Ready! Click Play to hear the word.';
+ }
+
+ playWord() {
+ const utterance = new SpeechSynthesisUtterance(this.currentWord);
+ utterance.rate = 0.8;
+ this.synth.speak(utterance);
+ }
+
+ checkWord() {
+ const userInput = this.input.value.trim().toLowerCase();
+ this.total++;
+
+ if (userInput === this.currentWord) {
+ this.correct++;
+ this.playSuccessSound();
+ this.feedback.textContent = 'Correct! 🎉';
+ setTimeout(() => this.getNewWord(), 1500);
+ } else {
+ this.playErrorSound();
+ this.feedback.textContent = 'Try again! 😕';
+ }
+
+ this.updateScore();
+ }
+
+ showHint() {
+ let hint = '';
+ const userInput = this.input.value.toLowerCase();
+
+ for (let i = 0; i < this.currentWord.length; i++) {
+ if (i >= userInput.length) {
+ hint += '_ ';
+ } else if (userInput[i] === this.currentWord[i]) {
+ hint += userInput[i] + ' ';
+ } else {
+ hint += '❌ ';
+ }
+ }
+
+ this.feedback.textContent = `Hint: ${hint}`;
+ }
+
+ updateScore() {
+ const percentage = Math.round((this.correct / this.total) * 100);
+ this.score.textContent = `${this.correct}/${this.total} (${percentage}%)`;
+ }
+
+ playSuccessSound() {
+ const audio = new Audio('data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLXrO7tF2MggbW67j6oFJGw9Bg8vr03s/GQwwYKvQ5pVqLA8hSYW84/CwhEMQFjFqp9f0xZhVGxIgTIjB8/bDik4VDxlB...');
+ audio.play();
+ }
+
+ playErrorSound() {
+ const audio = new Audio('data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLXrO7tF2MggbW67j6oFJGw9Bg8vr03s/GQwwYKvQ5pVqLA8hSYW84/CwhEMQFjFqp9f0xZhVGxIgTIjB8/bDik4VDxlB...');
+ audio.play();
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new SpellIt();
+ });