Primary 3 Unit 7 Quiz – Transport & Communication
Primary 3 – Unit 7 Quiz 🚦
Time Left: 45:00
Q${index+1}: ${q.q}
`;
q.a.forEach((ans,i)=>{
html += ``;
});
html += `
`;
document.getElementById('quizContainer').innerHTML = html;
}
showQuestion(currentQ);
// NAVIGATION
document.getElementById('prevBtn').onclick = () => {
saveAnswer();
if(currentQ > 0){
currentQ--;
showQuestion(currentQ);
}
};
document.getElementById('nextBtn').onclick = () => {
saveAnswer();
if(currentQ < questions.length - 1){
currentQ++;
showQuestion(currentQ);
}
};
// SAVE ANSWER
function saveAnswer(){
const selected = document.querySelector('input[name="answer"]:checked');
if(selected){
answers[currentQ] = parseInt(selected.value);
}
}
// LEADERBOARD
let leaderboard = JSON.parse(localStorage.getItem('leaderboard')) || [];
function updateLeaderboard(score){
const name = prompt("Enter your name:");
if(!name) return;
leaderboard.push({name, score});
leaderboard.sort((a,b)=>b.score-a.score);
leaderboard = leaderboard.slice(0,10);
localStorage.setItem('leaderboard', JSON.stringify(leaderboard));
displayLeaderboard();
}
function displayLeaderboard(){
const list = document.getElementById('leaderboardList');
list.innerHTML = '';
leaderboard.forEach(entry=>{
const li = document.createElement('li');
li.textContent = `${entry.name} - ${entry.score}/${questions.length}`;
list.appendChild(li);
});
}
displayLeaderboard();
// SUBMIT
document.getElementById('submitBtn').onclick = submitQuiz;
function submitQuiz(){
saveAnswer();
clearInterval(timerInterval);
let score = 0;
answers.forEach((ans,i)=>{
if(ans === questions[i].correct) score++;
});
document.getElementById('score').textContent =
`🎯 Your Score: ${score} / ${questions.length}`;
updateLeaderboard(score);
}