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

جواب «Step 4: The Score Screen» چالش 4 از جلسه‌ی 16

سؤال

قدم ۴: صفحه‌ی نمره. در پایان آزمون، #message بنویسد: همه درست → Perfect!، نصف یا بیشتر → Good job!، کمتر → Keep practising!. بهترین نمره را با salam.load("best", 0) بخوان، اگر نمره‌ی تازه بیشتر بود با salam.save("best", best) ذخیره کن، و همیشه (از همان باز شدن اپ) در #best بنویس Best: 2. به #again بده onclick="playAgain()" که صفحه‌ی play را نشان بدهد و آزمون را از اول شروع کند.

راهنما

«نصف یا بیشتر» یعنی score * 2 >= total. اول Perfect! را بسنج (score === total)، بعد با else if آن یکی را.

یک جواب درست

<!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" onclick="playAgain()">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();
  }
}

let best = salam.load("best", 0);
document.getElementById("best").textContent = "Best: " + best;

function finish() {
  playing = false;
  let total = questions.length;
  document.getElementById("result").textContent = score + " of " + total;
  let message = "Keep practising!";
  if (score === total) {
    message = "Perfect!";
  } else if (score * 2 >= total) {
    message = "Good job!";
  }
  document.getElementById("message").textContent = message;
  if (score > best) {
    best = score;
    salam.save("best", best);
  }
  document.getElementById("best").textContent = "Best: " + best;
  show("score");
}

function playAgain() {
  show("play");
  startQuiz();
}
</script>
</body>
</html>

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن