Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const wordInput = document.getElementById('wordInput');
+ const submitButton = document.getElementById('submitWord');
+ const previousWord = document.getElementById('previousWord');
+ const wordChain = document.getElementById('wordChain');
+ const scoreElement = document.getElementById('score');
+ const timerElement = document.getElementById('timer');
+
+ let score = 0;
+ let timeLeft = 60;
+ let usedWords = new Set();
+ let timer;
+
+ const basicWords = new Set([
+ 'apple', 'banana', 'cat', 'dog', 'elephant', 'fish',
+ 'goat', 'house', 'ice', 'juice', 'kite', 'lion',
+ 'monkey', 'nose', 'orange', 'pig', 'queen', 'rabbit',
+ 'snake', 'tiger', 'umbrella', 'van', 'water', 'box',
+ 'yellow', 'zebra'
+ ]);
+
+ function startGame() {
+ timer = setInterval(() => {
+ timeLeft--;
+ timerElement.textContent = timeLeft;
+
+ if (timeLeft <= 0) {
+ endGame();
+ }
+ }, 1000);
+ }
+
+ function endGame() {
+ clearInterval(timer);
+ wordInput.disabled = true;
+ submitButton.disabled = true;
+ alert(`Game Over! Your score: ${score}`);
+ }
+
+ function isValidWord(word) {
+ return basicWords.has(word.toLowerCase()) && !usedWords.has(word.toLowerCase());
+ }
+
+ function checkWordChain(prevWord, newWord) {
+ if (prevWord === 'Start!') return true;
+ return prevWord.slice(-1).toLowerCase() === newWord.charAt(0).toLowerCase();
+ }
+
+ function addWordToChain(word) {
+ const wordElement = document.createElement('span');
+ wordElement.textContent = word + ' → ';
+ wordChain.appendChild(wordElement);
+ previousWord.textContent = word;
+ usedWords.add(word.toLowerCase());
+ }
+
+ submitButton.addEventListener('click', () => {
+ const word = wordInput.value.trim();
+
+ if (!timer) {
+ startGame();
+ }
+
+ if (word && isValidWord(word) && checkWordChain(previousWord.textContent, word)) {
+ score += word.length;
+ scoreElement.textContent = score;
+ addWordToChain(word);
+ wordInput.value = '';
+ } else {
+ alert('Invalid word! Try again.');
+ }
+ });
+
+ wordInput.addEventListener('keypress', (e) => {
+ if (e.key === 'Enter') {
+ submitButton.click();
+ }
+ });
+ });