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

Step 2: Make Questions چالش 2 از جلسه‌ی 16، متوسط

قدم ۲: ساختن سؤال. به فرم #makeForm بده onsubmit="addQuestion(event)". یک آرایه‌ی questions بساز که با salam.load("questions", []) پر شود. addQuestion(event) جلوی ثبت فرم را بگیرد، #q و #a را (با trim) بخواند، اگر یکی خالی بود کاری نکند، وگرنه { q: ..., a: ... } را push کند، با salam.save("questions", questions) ذخیره کند، کادرها را خالی کند و renderList() را صدا بزند. renderList() هر سؤال را به شکل li با متن What is 2 + 3? = 5 در #qlist بکشد و در #qcount بنویسد Questions: 2. موقع باز شدن اپ هم renderList() اجرا شود.

<!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">
    <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");
</script>
</body>
</html>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 16: پروژه: آزمون‌ساز · چالش بعدی: Step 3: Take the Quiz

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