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

جواب «The Clock» چالش 1 از جلسه‌ی 8

سؤال

در #status قبل از #lives یک <span id="clock"></span> و زیر #status این نوار را بگذار: <div id="bar" class="bar"><i id="fill"></i></div>. در CSS نوار را بساز: .bar با height: 14px، margin-block: 12px، خط 2px solid var(--line)، border-radius: 99px، پس‌زمینه‌ی var(--card) و overflow: hidden؛ #fill با display: block، height: 100% و پس‌زمینه‌ی var(--accent)؛ و .bar.low #fill { background: #e5484d; }.
به CONFIG مقدار secondsPerQuestion: 20 را اضافه کن. متغیرهای timer و started را بساز. تابع tick() زمان مانده را حساب کند، در #clock بنویسد "⏱ " + num(...) (با Math.ceil و نه کمتر از صفر)، عرض #fill را درصدی کند و اگر ۵ ثانیه یا کمتر مانده low را روی #bar بگذارد. stopTimer() زمان‌سنج را متوقف کند.
آخر showQuestion این سه خط را بگذار: started = Date.now(); و tick(); و timer = setInterval(tick, 200);. اول choose هم stopTimer();.

راهنما

درصد نوار: Math.max(0, left / total) * 100 + "%".

یک جواب درست

<!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; }
</style>
</head>
<body>
<div class="app">
<header id="top">
  <h1 id="app-name">کوییز من</h1>
  <p id="tagline">موضوعت را انتخاب کن و خودت را بسنج</p>
</header>
<section id="home">
<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>
</div>
<script>
function num(n) {
  return n.toLocaleString("fa-IR");
}

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

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 timer = 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) {
    score += 10;
    byId("score").textContent = "⭐ " + num(score);
    msg.textContent = "آفرین! " + q.explain;
  } else {
    lives--;
    byId("lives").textContent = "❤️".repeat(lives) || "💔";
    msg.textContent = "این یکی نشد. " + q.explain;
    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) {
    showEnd();
  } else {
    showQuestion();
  }
}

function showEnd() {
  byId("question").textContent = "تمام شد!";
  byId("options").replaceChildren();
  byId("msg").textContent = "";
  byId("next").classList.add("hide");
}

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() {
  show("home");
  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;
  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);
}

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

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