جواب «Step 2: Show the Question» چالش 2 از جلسهی 12
سؤال
تابع show() بنویس (میتوانی از هوش مصنوعی بخواهی، ولی خط به خط بخوانش): متن سؤال questions[current] را در #question بگذارد، #answers را خالی کند و برای هر گزینه یک <button> بسازد که با کلیک pick(i) را صدا بزند. pick فعلاً فقط current را یکی زیاد کند و اگر سؤال مانده show() کند. کلیک روی #start هم current = 0 و show().
راهنما
item.options.forEach(function (text, i) { const b = document.createElement("button"); ... box.appendChild(b); });.
یک جواب درست
<h1>Know Your AI</h1>
<p id="question">Press Start!</p>
<div id="answers"></div>
<p id="score"></p>
<button id="start">Start</button>
<script>
const questions = [
{ q: "Can an AI chat bot make up facts?", options: ["Yes", "No", "Only on Fridays"], answer: 0 },
{ q: "Should you tell a chat bot your password?", options: ["Yes", "Only if it asks nicely", "Never"], answer: 2 },
{ q: "What is a deepfake?", options: ["A fake photo or voice made by AI", "A deep pool", "A kind of fish"], answer: 0 },
{ q: "Who should check what an AI says?", options: ["Nobody", "You, with a book or a teacher", "Another AI only"], answer: 1 },
{ q: "Using AI for homework is honest when...", options: ["You hide it", "It teaches you and you say so", "It writes everything"], answer: 1 },
];
let current = 0;
function show() {
const item = questions[current];
document.getElementById("question").textContent = item.q;
const box = document.getElementById("answers");
box.innerHTML = "";
item.options.forEach(function (text, i) {
const b = document.createElement("button");
b.textContent = text;
b.onclick = function () { pick(i); };
box.appendChild(b);
});
}
function pick(i) {
current = current + 1;
if (current < questions.length) {
show();
}
}
document.getElementById("start").onclick = function () {
current = 0;
document.getElementById("score").textContent = questions.length + " questions";
show();
};
</script>
<style>
body { font-family: sans-serif; padding: 16px; }
#answers { display: flex; flex-wrap: wrap; gap: 8px; }
button { min-height: 40px; padding: 8px 14px; font-size: 16px; }
</style>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن