جواب «Step 3: Take the Quiz» چالش 3 از جلسهی 16
سؤال
قدم ۳: گرفتن آزمون. به #startBtn بده onclick="startQuiz()" و به #answerForm بده onsubmit="checkAnswer(event)". startQuiz() مقدار index و score را صفر کند؛ اگر سؤالی نبود در #question بنویسد No questions yet. Make some first!، وگرنه سؤال اول را نشان بدهد: متن سؤال در #question و Question 1 of 3 در #progress. checkAnswer(event) جواب #answer را بیتوجه به فاصله و حروف بزرگ و کوچک با جواب درست مقایسه کند؛ اگر درست بود score یکی زیاد شود و #note بگوید Right!، وگرنه Wrong! The answer was Tehran. بعد کادر را خالی کند و سؤال بعد را نشان بدهد. بعد از سؤال آخر، #result بنویسد مثلاً 2 of 3 و show("score") صدا زده شود.
راهنما
یک تابع showQuestion() بنویس که #progress و #question را از روی index پر کند. در checkAnswer بعد از نمره دادن، index را یکی زیاد کن: اگر index < questions.length بود showQuestion()، وگرنه کار پایان آزمون.
یک جواب درست
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quiz Maker</title>
<style>
body { font-family: sans-serif; margin: 0; background-color: #f4f1ff; }
header { background-color: #6c4ad6; color: white; padding: 12px 16px; }
.screen { max-width: 420px; margin: 0 auto; padding: 16px 16px 90px; }
label { display: block; margin-top: 8px; }
input { font-size: 16px; min-height: 44px; width: 100%; box-sizing: border-box; }
.screen button { font-size: 16px; min-height: 44px; margin-top: 8px; }
nav { position: fixed; bottom: 0; left: 0; right: 0; display: flex; background-color: white; }
nav button { flex: 1; min-height: 56px; font-size: 16px; border: none; background-color: white; }
nav button.active { background-color: #ffd23f; font-weight: bold; }
</style>
</head>
<body>
<header><h1>Quiz Maker</h1></header>
<section class="screen" id="make">
<h2>Make questions</h2>
<form id="makeForm" onsubmit="addQuestion(event)">
<label for="q">Question</label>
<input id="q" placeholder="What is 2 + 3?">
<label for="a">Right answer</label>
<input id="a" placeholder="5">
<button>Add question</button>
</form>
<p id="qcount">Questions: 0</p>
<ul id="qlist"></ul>
</section>
<section class="screen" id="play">
<h2>Quiz</h2>
<button id="startBtn" type="button" onclick="startQuiz()">Start</button>
<p id="progress"></p>
<p id="time"></p>
<p id="question">Press Start!</p>
<form id="answerForm" onsubmit="checkAnswer(event)">
<label for="answer">Your answer</label>
<input id="answer">
<button>Check</button>
</form>
<p id="note"></p>
</section>
<section class="screen" id="score">
<h2>Score</h2>
<p id="result">No quiz yet</p>
<p id="message"></p>
<p id="best"></p>
<button id="again" type="button">Play again</button>
</section>
<nav>
<button id="tab-make" onclick="show('make')">Make</button>
<button id="tab-play" onclick="show('play')">Play</button>
<button id="tab-score" onclick="show('score')">Score</button>
</nav>
<script>
function show(name) {
let screens = document.querySelectorAll(".screen");
for (let screen of screens) {
screen.style.display = "none";
}
document.getElementById(name).style.display = "block";
let tabs = document.querySelectorAll("nav button");
for (let tab of tabs) {
tab.classList.remove("active");
}
document.getElementById("tab-" + name).classList.add("active");
}
show("make");
let questions = salam.load("questions", []);
function renderList() {
let list = document.getElementById("qlist");
list.innerHTML = "";
for (let item of questions) {
let li = document.createElement("li");
li.textContent = item.q + " = " + item.a;
list.appendChild(li);
}
document.getElementById("qcount").textContent = "Questions: " + questions.length;
}
function addQuestion(event) {
event.preventDefault();
let q = document.getElementById("q").value.trim();
let a = document.getElementById("a").value.trim();
if (q === "" || a === "") {
return;
}
questions.push({ q: q, a: a });
salam.save("questions", questions);
document.getElementById("q").value = "";
document.getElementById("a").value = "";
renderList();
}
renderList();
let index = 0;
let score = 0;
let playing = false;
function startQuiz() {
index = 0;
score = 0;
document.getElementById("note").textContent = "";
if (questions.length === 0) {
document.getElementById("question").textContent = "No questions yet. Make some first!";
return;
}
playing = true;
showQuestion();
}
function showQuestion() {
document.getElementById("progress").textContent = "Question " + (index + 1) + " of " + questions.length;
document.getElementById("question").textContent = questions[index].q;
}
function checkAnswer(event) {
event.preventDefault();
if (!playing) {
return;
}
let answer = document.getElementById("answer").value.trim().toLowerCase();
let right = questions[index].a;
if (answer === right.toLowerCase()) {
score = score + 1;
document.getElementById("note").textContent = "Right!";
} else {
document.getElementById("note").textContent = "Wrong! The answer was " + right;
}
document.getElementById("answer").value = "";
nextQuestion();
}
function nextQuestion() {
index = index + 1;
if (index < questions.length) {
showQuestion();
} else {
finish();
}
}
function finish() {
playing = false;
document.getElementById("result").textContent = score + " of " + questions.length;
show("score");
}
</script>
</body>
</html>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن