جواب «Step 3: Score and the Final Message» چالش 3 از جلسهی 12
سؤال
امتیاز: let score = 0. در pick(i)، اگر i === questions[current].answer بود امتیاز یکی زیاد شود و #score بنویسد Score: 2 (با عدد). بعد سؤال بعد؛ و وقتی سؤالها تمام شد، #question بنویسد Done! Score: 3 / 5 (با عددهای واقعی) و #answers خالی شود. کلیک روی #start امتیاز را هم صفر کند.
راهنما
در else بعد از if (current < questions.length): "Done! Score: " + score + " / " + questions.length.
یک جواب درست
<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 score = 0;
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) {
if (i === questions[current].answer) {
score = score + 1;
}
document.getElementById("score").textContent = "Score: " + score;
current = current + 1;
if (current < questions.length) {
show();
} else {
document.getElementById("question").textContent = "Done! Score: " + score + " / " + questions.length;
document.getElementById("answers").innerHTML = "";
}
}
document.getElementById("start").onclick = function () {
current = 0;
score = 0;
document.getElementById("score").textContent = "Score: 0";
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>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن