Changed around line 1
+ class SpellIt {
+ constructor() {
+ this.words = [];
+ this.currentWord = '';
+ this.correct = 0;
+ this.total = 0;
+ this.synth = window.speechSynthesis;
+
+ this.loadWords();
+ this.initializeElements();
+ this.addEventListeners();
+ }
+
+ async loadWords() {
+ 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);
+ } catch (error) {
+ console.error('Error loading words:', error);
+ this.words = ['hello', 'world', 'javascript', 'programming']; // Fallback words
+ }
+ }
+
+ initializeElements() {
+ this.display = document.querySelector('.word-display');
+ this.input = document.getElementById('word-input');
+ this.playBtn = document.getElementById('play-btn');
+ this.enterBtn = document.getElementById('enter-btn');
+ this.hintBtn = document.getElementById('hint-btn');
+ this.correctCount = document.getElementById('correct-count');
+ this.totalCount = document.getElementById('total-count');
+ this.successRate = document.getElementById('success-rate');
+ }
+
+ addEventListeners() {
+ this.playBtn.addEventListener('click', () => this.playWord());
+ this.enterBtn.addEventListener('click', () => this.checkWord());
+ this.hintBtn.addEventListener('click', () => this.showHint());
+ this.input.addEventListener('keypress', (e) => {
+ if (e.key === 'Enter') this.checkWord();
+ });
+ }
+
+ playWord() {
+ if (!this.currentWord) {
+ this.currentWord = this.words[Math.floor(Math.random() * this.words.length)];
+ }
+
+ const utterance = new SpeechSynthesisUtterance(this.currentWord);
+ utterance.rate = 0.8;
+ this.synth.speak(utterance);
+ }
+
+ checkWord() {
+ if (!this.currentWord) return;
+
+ this.total++;
+ const userInput = this.input.value.toLowerCase().trim();
+
+ if (userInput === this.currentWord) {
+ this.correct++;
+ this.playSound(true);
+ this.display.textContent = 'Correct! 🎉';
+ this.display.style.color = '#2ecc71';
+ } else {
+ this.playSound(false);
+ this.display.textContent = 'Try again! 🔄';
+ this.display.style.color = '#e74c3c';
+ }
+
+ this.updateStats();
+ this.input.value = '';
+ setTimeout(() => this.nextWord(), 2000);
+ }
+
+ showHint() {
+ if (!this.currentWord) return;
+
+ const userInput = this.input.value.toLowerCase();
+ let hint = '';
+
+ for (let i = 0; i < this.currentWord.length; i++) {
+ if (i < userInput.length) {
+ if (userInput[i] === this.currentWord[i]) {
+ hint += this.currentWord[i];
+ } else {
+ hint += '❌';
+ }
+ } else {
+ hint += '_';
+ }
+ }
+
+ this.display.textContent = hint;
+ }
+
+ playSound(success) {
+ const audioContext = new (window.AudioContext || window.webkitAudioContext)();
+ const oscillator = audioContext.createOscillator();
+ const gainNode = audioContext.createGain();
+
+ oscillator.connect(gainNode);
+ gainNode.connect(audioContext.destination);
+
+ oscillator.type = success ? 'sine' : 'square';
+ oscillator.frequency.setValueAtTime(success ? 800 : 300, audioContext.currentTime);
+
+ gainNode.gain.setValueAtTime(0.3, audioContext.currentTime);
+ gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + 0.5);
+
+ oscillator.start();
+ oscillator.stop(audioContext.currentTime + 0.5);
+ }
+
+ updateStats() {
+ this.correctCount.textContent = this.correct;
+ this.totalCount.textContent = this.total;
+ this.successRate.textContent = `${Math.round((this.correct / this.total) * 100)}%`;
+ }
+
+ nextWord() {
+ this.currentWord = this.words[Math.floor(Math.random() * this.words.length)];
+ this.display.textContent = 'Press PLAY for next word';
+ this.display.style.color = '#4ecdc4';
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new SpellIt();
+ });