بتل کد Battle Codeدارد باز می‌شود…

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") صدا زده شود.

<!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">Start</button>
  <p id="progress"></p>
  <p id="time"></p>
  <p id="question">Press Start!</p>
  <form id="answerForm">
    <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();
</script>
</body>
</html>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 16: پروژه: آزمون‌ساز · چالش بعدی: Step 4: The Score Screen

بیشتر بخوان: جاوااسکریپت چیست؟ از دکمه‌ی وب تا Node.js