Word Meaning and Verb Form Quiz
Word Meaning and Verb Form Quiz
Score: 0
// Add more questions here, each followed by a comma
];
let score = 0;
let currentQuestionIndex = 0;
let usedQuestions = [];
function showNextQuestion() {
document.getElementById('next-button').style.display = 'none'; // Hide the Next button
document.getElementById('answer').innerText = ''; // Clear the answer text
document.getElementById('avatar').style.display = 'none'; // Hide the avatar
if (usedQuestions.length === questions.length) {
showResult();
return;
}
let randomIndex;
do {
randomIndex = Math.floor(Math.random() * questions.length);
} while (usedQuestions.includes(randomIndex));
usedQuestions.push(randomIndex);
currentQuestionIndex = randomIndex;
const questionObj = questions[randomIndex];
document.getElementById('question').innerText = questionObj.question;
const optionsContainer = document.getElementById('options');
optionsContainer.innerHTML = '';
questionObj.options.forEach((option, index) => {
const optionButton = document.createElement('button');
optionButton.innerText = option.answer;
optionButton.onclick = () => checkAnswer(option, questionObj.correctMessage);
const listItem = document.createElement('li');
listItem.appendChild(optionButton);
optionsContainer.appendChild(listItem);
});
}
function checkAnswer(option, message) {
const answerElement = document.getElementById('answer');
const optionButtons = document.querySelectorAll('.options li button');
if (option.isCorrect) {
score++;
answerElement.innerText = 'Correct: ' + message;
optionButtons.forEach(btn => {
if (btn.innerText === option.answer) {
btn.classList.add('correct');
}
});
} else {
answerElement.innerText = 'Incorrect: ' + message;
optionButtons.forEach(btn => {
if (btn.innerText === option.answer) {
btn.classList.add('incorrect');
} else if (questions[currentQuestionIndex].options.find(opt => opt.isCorrect).answer === btn.innerText) {
btn.classList.add('correct');
}
});
}
document.getElementById('avatar').style.display = 'block';
document.getElementById('next-button').style.display = 'block'; // Show the Next button
updateScore();
readAnswer(message);
}
function readAnswer(message) {
const speech = new SpeechSynthesisUtterance(message);
speech.voice = speechSynthesis.getVoices().find(voice => voice.name.includes("Google UK English Female"));
speechSynthesis.speak(speech);
}
function updateScore() {
document.getElementById('score').innerText = `Score: ${score}`;
}
function showResult() {
const resultMessage = `You have completed the quiz! Your score is ${score} out of ${questions.length}.`;
document.getElementById('question').innerText = resultMessage;
document.getElementById('options').innerHTML = '';
document.getElementById('avatar').style.display = 'block';
document.getElementById('next-button').style.display = 'none'; // Hide the Next button
document.getElementById('try-again-button').style.display = 'block'; // Show the Try Again button
if (!speechSynthesis.speaking) {
readAnswer(resultMessage);
}
}
function resetQuiz() {
score = 0;
currentQuestionIndex = 0;
usedQuestions = [];
document.getElementById('score').innerText = `Score: ${score}`;
document.getElementById('try-again-button').style.display = 'none'; // Hide the Try Again button
showNextQuestion();
}
window.onload = showNextQuestion;