Randomized True or False Quiz
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 20px;
}
.question-box {
margin: 20px;
}
.score-box {
font-size: 18px;
margin-bottom: 10px;
}
.true-false-buttons {
display: flex;
justify-content: center;
gap: 20px;
}
True or False Quiz
Correct: 0 | Wrong: 0
True
False
// Sample question data (array of objects)
const questions = [
{
image: “path/to/image1.jpg”,
text: “Is the sky blue?”,
answer: true
},
{
image: “path/to/image2.jpg”,
text: “Is the Earth flat?”,
answer: false
},
{
image: “path/to/image3.jpg”,
text: “Do cats bark?”,
answer: false
},
{
image: “path/to/image4.jpg”,
text: “Is water wet?”,
answer: true
}
];
// Load stored score values
let correctScore = localStorage.getItem(‘correctScore’) || 0;
let wrongScore = localStorage.getItem(‘wrongScore’) || 0;
// Display the scores
document.getElementById(‘correct-score’).innerText = correctScore;
document.getElementById(‘wrong-score’).innerText = wrongScore;
// Randomly select a question
let currentQuestionIndex = Math.floor(Math.random() * questions.length);
let currentQuestion = questions[currentQuestionIndex];
// Display the randomly selected question and image
document.getElementById(‘question-image’).src = currentQuestion.image;
document.getElementById(‘question-text’).innerText = currentQuestion.text;
function submitAnswer(userAnswer) {
let feedback = document.getElementById(‘feedback’);
// Check if the answer is correct
if (userAnswer === currentQuestion.answer) {
correctScore++;
feedback.innerHTML = “
Correct!“;
} else {
wrongScore++;
feedback.innerHTML = “
Wrong!“;
}
// Update the scores
document.getElementById(‘correct-score’).innerText = correctScore;
document.getElementById(‘wrong-score’).innerText = wrongScore;
// Store the updated scores in localStorage
localStorage.setItem(‘correctScore’, correctScore);
localStorage.setItem(‘wrongScore’, wrongScore);
// After showing feedback, load a new random question
setTimeout(() => {
window.location.reload(); // Reload the page for a new random question
}, 1000);
}