const questions = [
{
question: "Ada 2 apel dan 3 pisang. Berapa jumlah semuanya?",
options: [3, 4, 5],
answer: 5
},
{
question: "Angka manakah yang lebih besar: 4 atau 2?",
options: [2, 4],
answer: 4
},
{
question: "Berapa hasil dari 1 + 2?",
options: [2, 3, 4],
answer: 3
}
];
let currentQuestion = 0;
function loadQuestion() {
const q = questions[currentQuestion];
document.getElementById("question").innerText = q.question;
const optionsDiv = document.getElementById("options");
optionsDiv.innerHTML = "";
q.options.forEach(option => {
const btn = document.createElement("button");
btn.innerText = option;
btn.classList.add("btn", "btn-outline-primary", "m-2");
btn.onclick = () => checkAnswer(option);
optionsDiv.appendChild(btn);
});
}
function checkAnswer(selected) {
const feedback = document.getElementById("feedback");
if (selected === questions[currentQuestion].answer) {
feedback.innerText = "Benar!";
feedback.style.color = "green";
} else {
feedback.innerText = "Salah, coba lagi.";
feedback.style.color = "red";
}
}
function nextQuestion() {
if (currentQuestion < questions.length - 1) {
currentQuestion++;
loadQuestion();
document.getElementById("feedback").innerText = "";
} else {
document.getElementById("question").innerText = "Selesai! Semua soal telah dijawab.";
document.getElementById("options").innerHTML = "";
document.getElementById("feedback").innerText = "";
}
}
window.onload = loadQuestion;