const wordPools = {
1: [‘mitochondria’, ‘chloroplast’, ‘ribosome’, ‘nucleus’, ‘lysosome’], // Chapter 1 pool
2: [‘allele’, ‘genotype’, ‘phenotype’, ‘chromosome’, ‘mutation’], // Chapter 2 pool
3: [‘natural selection’, ‘adaptation’, ‘speciation’, ‘fossils’, ‘mutation’] // Chapter 3 pool
};
const feedbackCorrect = [“Excellent!”, “Correct!”, “Bravo!”, “Good shot!”, “Superb!”];
const feedbackWrong = [“Try again!”, “Not quite right!”, “Almost, try again!”, “Keep going!”];
let selectedWords = [];
let currentWord = ”;
let timer = 0;
let wordDisappearTimer;
let difficultyTime = 15;
let wordCount = 0;
let score = 0;
let maxWords = 20;
let gameOver = false;
const chapterSelect = document.getElementById(‘chapter-select’);
const difficultySelect = document.getElementById(‘difficulty-select’);
const wordDisplay = document.getElementById(‘word-display’);
const typedWordInput = document.getElementById(‘typed-word’);
const checkSpellingBtn = document.getElementById(‘check-spelling-btn’);
const skipWordBtn = document.getElementById(‘skip-word-btn’);
const showWordBtn = document.getElementById(‘show-word-btn’);
const resultDisplay = document.getElementById(‘result’);
const timerDisplay = document.getElementById(‘timer’);
const scoreDisplay = document.getElementById(‘score’);
const gameOverDisplay = document.getElementById(‘game-over’);
const startBtn = document.getElementById(‘start-btn’);
const resetBtn = document.getElementById(‘reset-btn’);
// Shuffle the array
function shuffleArray(array) {
return array.sort(() => 0.5 – Math.random());
}
// Difficulty level control
function setDifficultyTime() {
const difficulty = difficultySelect.value;
if (difficulty === ‘easy’) {
difficultyTime = 15;
} else if (difficulty === ‘medium’) {
difficultyTime = 10;
} else if (difficulty === ‘hard’) {
difficultyTime = 5;
}
}
// Start the game
startBtn.addEventListener(‘click’, () => {
const selectedChapter = chapterSelect.value;
selectedWords = shuffleArray(wordPools[selectedChapter]).slice(0, maxWords); // Select 20 random words
score = 0;
wordCount = 0;
gameOver = false;
updateScore();
nextWord();
enableGameElements(true);
gameOverDisplay.style.display = ‘none’; // Hide game over message
resetBtn.style.display = ‘inline-block’; // Show reset button
startBtn.style.display = ‘none’; // Hide start button
});
// Enable or disable game elements
function enableGameElements(enable) {
typedWordInput.disabled = !enable;
checkSpellingBtn.disabled = !enable;
startBtn.disabled = enable;
}
// Reset the game
resetBtn.addEventListener(‘click’, () => {
selectedWords = [];
currentWord = ”;
score = 0;
wordCount = 0;
gameOver = false;
enableGameElements(false);
resultDisplay.textContent = ”;
wordDisplay.textContent = ”;
typedWordInput.value = ”;
gameOverDisplay.style.display = ‘none’;
resetBtn.style.display = ‘none’; // Hide reset button
startBtn.style.display = ‘inline-block’; // Show start button
scoreDisplay.textContent = ‘Score: 0’; // Reset score display
timerDisplay.textContent = ‘Time: 0s’; // Reset timer display
});
// Hide the word after a delay based on difficulty
function hideWordAfterDelay() {
wordDisappearTimer = setTimeout(() => {
if (!gameOver) { // Ensure the game is not over before hiding the word
wordDisplay.textContent = ”; // Hide the word
}
}, difficultyTime * 1000);
}
// Update score
function updateScore() {
scoreDisplay.textContent = `Score: ${score}`;
}
// Check spelling when the button is pressed
checkSpellingBtn.addEventListener(‘click’, () => {
stopTimer(); // Stop the clock when the user checks spelling
const typedWord = typedWordInput.value.trim().toLowerCase();
if (typedWord === currentWord.toLowerCase()) {
const randomFeedback = feedbackCorrect[Math.floor(Math.random() * feedbackCorrect.length)];
resultDisplay.textContent = randomFeedback;
score += 1; // Increment score for correct answer
updateScore();
nextWord();
} else {
const randomFeedback = feedbackWrong[Math.floor(Math.random() * feedbackWrong.length)];
resultDisplay.textContent = randomFeedback;
skipWordBtn.style.display = ‘inline-block’; // Show the skip button
showWordBtn.style.display = ‘inline-block’; // Show the show word button
}
});
// Skip the word
skipWordBtn.addEventListener(‘click’, () => {
nextWord();
skipWordBtn.style.display = ‘none’; // Hide the skip button
showWordBtn.style.display = ‘none’; // Hide the show word button
});
// Show the correct word
showWordBtn.addEventListener(‘click’, () => {
resultDisplay.textContent = `The correct word was: ${currentWord}`;
});
// Display the next word
function nextWord() {
if (wordCount < maxWords) {
currentWord = selectedWords[wordCount];
wordCount++;
wordDisplay.textContent = currentWord;
typedWordInput.value = '';
resultDisplay.textContent = '';
setDifficultyTime();
startTimer();
hideWordAfterDelay();
skipWordBtn.style.display = 'none'; // Hide the skip button
showWordBtn.style.display = 'none'; // Hide the show word button
} else {
endGame();
}
}
// Start the timer for the word
function startTimer() {
timer = 0;
clearInterval(wordDisappearTimer); // Clear previous timer
wordDisappearTimer = setInterval(() => {
timer++;
timerDisplay.textContent = `Time: ${timer}s`;
}, 1000);
}
// Stop the timer when check spelling is pressed
function stopTimer() {
clearTimeout(wordDisappearTimer); // Clear the hide word timer
clearInterval(wordDisappearTimer); // Clear the timer interval
}
// End the game
function endGame() {
gameOver = true;
enableGameElements(false);
gameOverDisplay.style.display = ‘block’;
gameOverDisplay.textContent = `Game Over! Your final score is: ${score}`;
}