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

جواب «A New Record!» چالش 2 از جلسه‌ی 9

سؤال

در #result زیر #final یک <p id="best-line"></p> بگذار. در showResult متغیر const record = score > best; را بساز. اگر رکورد زده شد، بنویس «رکورد تازه زدی! 🏆»؛ وگرنه «بهترین امتیاز تو: ۱۲۰» با عدد واقعی.

راهنما

record را قبل از عوض کردن best حساب کن.

یک جواب درست

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>کوییز من</title>
<style>
:root {
  --bg: #fff4dd;
  --card: #ffffff;
  --text: #24163f;
  --line: #24163f;
  --main: #6c3cf0;
  --on-main: #ffffff;
  --accent: #ffc531;
}
* { box-sizing: border-box; }
.app {
  width: min(680px, 100%);
  margin-inline: auto;
  padding: 16px;
}
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Vazirmatn, Tahoma, sans-serif;
  line-height: 1.7;
}
#top {
  padding: 24px 16px;
  border: 3px solid var(--line);
  border-radius: 20px;
  background: var(--main);
  color: var(--on-main);
  text-align: center;
}
h1, h2, p { margin: 0; overflow-wrap: anywhere; }
#question {
  margin-block: 16px;
  padding: 18px;
  border: 3px solid var(--line);
  border-radius: 16px;
  background: var(--card);
}
.option {
  width: 100%;
  min-height: 44px;
  padding: 10px 14px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  color: inherit;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
#options { display: grid; gap: 12px; }
#pick-title { margin-block: 16px 10px; font-size: 19px; }
#topics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(140px, 100%), 1fr));
  gap: 14px;
}
.topic {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
  min-height: 44px;
  padding: 14px 10px;
  border: 2px solid var(--line);
  border-radius: 16px;
  background: var(--card);
  color: inherit;
  font: inherit;
  cursor: pointer;
}
.topic { border-top: 10px solid var(--tint); }
.topic .icon { font-size: 34px; line-height: 1.3; }
.option.right { background: #16a56b; color: #06281a; }
.option.wrong { background: #e5484d; color: #300a0c; }
.hide { display: none; }
#msg { margin-block: 14px; font-weight: 700; }
.next {
  width: 100%;
  min-height: 48px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: var(--main);
  color: var(--on-main);
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}
.status {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 10px;
  margin-block-start: 16px;
  font-weight: 800;
}
.topic small { opacity: 0.75; }
.bar {
  height: 14px;
  margin-block: 12px;
  border: 2px solid var(--line);
  border-radius: 99px;
  background: var(--card);
  overflow: hidden;
}
#fill { display: block; height: 100%; background: var(--accent); }
.bar.low #fill { background: #e5484d; }
#result { text-align: center; margin-block-start: 22px; }
#stars { font-size: 48px; letter-spacing: 4px; color: var(--main); }
#final { font-size: clamp(48px, 15vw, 72px); font-weight: 900; line-height: 1.2; color: var(--main); }
.actions { display: flex; flex-wrap: wrap; gap: 12px; margin-block-start: 18px; }
.actions .next { flex: 1 1 150px; min-width: 0; }
#review { display: grid; gap: 10px; margin-block-start: 22px; text-align: start; }
.miss {
  padding: 12px 14px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--card);
}
.miss small { display: block; }
#best { margin-block-start: 14px; text-align: center; font-weight: 800; }
</style>
</head>
<body>
<div class="app">
<header id="top">
  <h1 id="app-name">کوییز من</h1>
  <p id="tagline">موضوعت را انتخاب کن و خودت را بسنج</p>
</header>
<section id="home">
<p id="best"></p>
<h2 id="pick-title">یک موضوع انتخاب کن</h2>
<div id="topics"></div>
</section>
<section id="play" class="hide">
<div id="status" class="status"><span id="counter"></span><span id="score"></span><span id="clock"></span><span id="lives"></span></div>
<div id="bar" class="bar"><i id="fill"></i></div>
<h2 id="question"></h2>
<div id="options"></div>
<p id="msg"></p>
<button id="next" class="next hide">سؤال بعدی</button>
</section>
<section id="result" class="hide">
  <div id="stars"></div>
  <div id="final"></div>
  <p id="best-line"></p>
  <p id="right-count"></p>
  <div class="actions">
    <button id="again" class="next">دوباره بازی کن</button>
    <button id="to-home" class="next">خانه</button>
  </div>
  <div id="review"></div>
</section>
</div>
<script>
function num(n) {
  return n.toLocaleString("fa-IR");
}

const CONFIG = { questionsPerGame: 4, lives: 3, secondsPerQuestion: 20 };
const SCREENS = ["home", "play", "result"];

function byId(id) {
  return document.getElementById(id);
}

const TOPICS = [
  {"id": "science", "title": "علوم", "icon": "🔬", "color": "#22b8a6", "questions": [
    {"text": "آب در فشار معمولی در چند درجه می‌جوشد؟", "options": ["۵۰", "۸۰", "۱۰۰", "۱۵۰"], "answer": 2, "level": "easy", "explain": "آب در فشار معمولی در ۱۰۰ درجه می‌جوشد."},
    {"text": "گیاهان برای ساختن غذا از کدام گاز هوا استفاده می‌کنند؟", "options": ["اکسیژن", "کربن‌دی‌اکسید", "نیتروژن", "هلیوم"], "answer": 1, "level": "easy", "explain": "گیاه کربن‌دی‌اکسید می‌گیرد و اکسیژن پس می‌دهد."},
    {"text": "بزرگ‌ترین اندام بدن انسان چیست؟", "options": ["قلب", "کبد", "پوست", "مغز"], "answer": 2, "level": "medium", "explain": "پوست تمام بدن را می‌پوشاند."},
    {"text": "کدام فلز در دمای اتاق مایع است؟", "options": ["آهن", "جیوه", "مس", "طلا"], "answer": 1, "level": "medium", "explain": "جیوه در دمای اتاق مایع است."},
    {"text": "نور خورشید حدوداً چند دقیقه طول می‌کشد تا به زمین برسد؟", "options": ["۱", "۸", "۳۰", "۶۰"], "answer": 1, "level": "hard", "explain": "نور خورشید حدود ۸ دقیقه در راه است."},
  ]},
  {"id": "space", "title": "فضا", "icon": "🚀", "color": "#6c3cf0", "questions": [
    {"text": "نزدیک‌ترین سیاره به خورشید کدام است؟", "options": ["زهره", "تیر", "مریخ", "زمین"], "answer": 1, "level": "easy", "explain": "تیر (عطارد) از همه به خورشید نزدیک‌تر است."},
    {"text": "بزرگ‌ترین سیاره‌ی منظومه‌ی شمسی کدام است؟", "options": ["زحل", "نپتون", "مشتری", "زمین"], "answer": 2, "level": "easy", "explain": "مشتری از همه‌ی سیاره‌ها بزرگ‌تر است."},
    {"text": "زمین در حدود چند روز یک بار دور خورشید می‌چرخد؟", "options": ["۳۰", "۱۰۰", "۲۴۵", "۳۶۵"], "answer": 3, "level": "easy", "explain": "یک دور زمین حدود ۳۶۵ روز است."},
    {"text": "کدام سیاره را «سیاره‌ی سرخ» می‌نامند؟", "options": ["مریخ", "مشتری", "تیر", "زهره"], "answer": 0, "level": "easy", "explain": "خاک مریخ زنگ‌زده و سرخ است."},
    {"text": "حلقه‌های معروف و پررنگ دور کدام سیاره است؟", "options": ["زمین", "تیر", "زحل", "مریخ"], "answer": 2, "level": "medium", "explain": "حلقه‌های زحل از یخ و سنگ ساخته شده‌اند."},
  ]},
  {"id": "math", "title": "ریاضی", "icon": "🔢", "color": "#ff8a3d", "questions": [
    {"text": "حاصل ۷ × ۸ چند است؟", "options": ["۴۸", "۵۴", "۵۶", "۶۳"], "answer": 2, "level": "easy", "explain": "۷ × ۸ می‌شود ۵۶."},
    {"text": "مجموع زاویه‌های هر مثلث چند درجه است؟", "options": ["۹۰", "۱۸۰", "۲۷۰", "۳۶۰"], "answer": 1, "level": "easy", "explain": "زاویه‌های هر مثلث روی هم ۱۸۰ درجه‌اند."},
    {"text": "اولین عدد اول بعد از ۱۰ کدام است؟", "options": ["۱۱", "۱۲", "۱۳", "۱۵"], "answer": 0, "level": "medium", "explain": "۱۱ فقط بر ۱ و خودش بخش‌پذیر است."},
    {"text": "۲۵٪ عدد ۸۰ چند است؟", "options": ["۱۰", "۱۵", "۲۰", "۲۵"], "answer": 2, "level": "medium", "explain": "یک‌چهارم ۸۰ می‌شود ۲۰."},
    {"text": "جذر ۸۱ چند است؟", "options": ["۷", "۸", "۹", "۱۱"], "answer": 2, "level": "medium", "explain": "۹ × ۹ می‌شود ۸۱."},
  ]},
];
let questions = [];
let index = 0;
let score = 0;
let lives = 0;
let best = salam.load("quiz.best", 0);
let timer = null;
let missed = [];
let topicNow = null;
let started = 0;

function showQuestion() {
  const q = questions[index];
  byId("counter").textContent = "سؤال " + num(index + 1) + " از " + num(questions.length);
  byId("question").textContent = q.text;
  byId("score").textContent = "⭐ " + num(score);
  byId("lives").textContent = "❤️".repeat(lives);
  byId("msg").textContent = "";
  byId("next").classList.add("hide");
  const box = byId("options");
  box.replaceChildren();
  for (let i = 0; i < q.options.length; i++) {
    const b = document.createElement("button");
    b.className = "option";
    b.textContent = q.options[i];
    b.addEventListener("click", () => choose(i));
    box.append(b);
  }
  started = Date.now();
  tick();
  timer = setInterval(tick, 200);
}

function choose(chosen) {
  stopTimer();
  const q = questions[index];
  const msg = byId("msg");
  const buttons = document.querySelectorAll(".option");
  buttons[q.answer].classList.add("right");
  if (chosen === q.answer) {
    const left = Math.max(0, CONFIG.secondsPerQuestion - Math.floor((Date.now() - started) / 1000));
    score += 10 + left;
    byId("score").textContent = "⭐ " + num(score);
    msg.textContent = "آفرین! " + q.explain;
  } else {
    lives--;
    missed.push(q);
    byId("lives").textContent = "❤️".repeat(lives) || "💔";
    msg.textContent = (chosen < 0 ? "وقت تمام شد. " : "این یکی نشد. ") + q.explain;
    if (chosen >= 0) {
      buttons[chosen].classList.add("wrong");
    }
  }
  for (const b of buttons) {
    b.disabled = true;
  }
  const last = index + 1 >= questions.length || lives <= 0;
  byId("next").textContent = last ? "دیدن نتیجه" : "سؤال بعدی";
  byId("next").classList.remove("hide");
}

function nextQuestion() {
  index++;
  if (index >= questions.length || lives <= 0) {
    showResult();
  } else {
    showQuestion();
  }
}

function showResult() {
  stopTimer();
  show("result");
  const record = score > best;
  if (record) {
    best = score;
    salam.save("quiz.best", best);
  }
  byId("best-line").textContent = record ? "رکورد تازه زدی! 🏆" : "بهترین امتیاز تو: " + num(best);
  const right = index - missed.length;
  const part = right / questions.length;
  const stars = part >= 0.9 ? 3 : part >= 0.6 ? 2 : part > 0 ? 1 : 0;
  byId("stars").textContent = "★".repeat(stars) + "☆".repeat(3 - stars);
  byId("final").textContent = num(score);
  byId("right-count").textContent = num(right) + " جواب درست از " + num(questions.length);
  const review = byId("review");
  review.replaceChildren();
  for (const q of missed) {
    const row = el("div", "miss", "");
    row.append(el("b", "", q.text), el("small", "", "جواب: " + q.options[q.answer]));
    review.append(row);
  }
}

function el(tag, className, text) {
  const node = document.createElement(tag);
  node.className = className;
  node.textContent = text;
  return node;
}

function show(name) {
  for (const id of SCREENS) {
    byId(id).classList.toggle("hide", id !== name);
  }
}

function showHome() {
  stopTimer();
  show("home");
  byId("best").textContent = best ? "🏆 بهترین امتیاز تو: " + num(best) : "";
  const grid = byId("topics");
  grid.replaceChildren();
  for (const topic of TOPICS) {
    const card = el("button", "topic", "");
    card.style.setProperty("--tint", topic.color);
    card.append(el("span", "icon", topic.icon), el("b", "", topic.title));
    card.append(el("small", "", num(topic.questions.length) + " سؤال"));
    card.addEventListener("click", () => startGame(topic));
    grid.append(card);
  }
}

function startGame(topic) {
  questions = shuffle(topic.questions).slice(0, CONFIG.questionsPerGame);
  index = 0;
  score = 0;
  lives = CONFIG.lives;
  missed = [];
  topicNow = topic;
  show("play");
  showQuestion();
}

function shuffle(list) {
  const copy = [...list];
  for (let i = copy.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [copy[i], copy[j]] = [copy[j], copy[i]];
  }
  return copy;
}

function stopTimer() {
  clearInterval(timer);
  timer = null;
}

function tick() {
  const total = CONFIG.secondsPerQuestion;
  const left = total - (Date.now() - started) / 1000;
  byId("clock").textContent = "⏱ " + num(Math.max(0, Math.ceil(left)));
  byId("fill").style.width = Math.max(0, left / total) * 100 + "%";
  byId("bar").classList.toggle("low", left <= 5);
  if (left <= 0) {
    choose(-1);
  }
}
byId("to-home").addEventListener("click", showHome);
byId("again").addEventListener("click", () => startGame(topicNow));

byId("next").addEventListener("click", nextQuestion);
showHome();
</script>
</body>
</html>

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