Topics from Data چالش 1 از جلسهی 7، سخت
دو صفحه بساز: <section id="home"> دور #pick-title و #topics، و <section id="play" class="hide"> دور بقیه (از #status تا #next). سه دکمهی موضوع را از HTML بردار تا #topics خالی شود.
آرایهی questions را با TOPICS عوض کن: سه موضوع با id، title، icon، color و questions؛ به ترتیب علوم 🔬، فضا 🚀 و ریاضی 🔢 با پنج سؤال در هر کدام. پاسخ این قدم، سؤالهای نمونه را آماده دارد. let questions = []; جایش بیاید.
ثابت SCREENS و توابع el، show، showHome (ساختن کارتها) و startGame(topic) را بساز. هر کارت یک button.topic است با span.icon، b و small که بگوید مثلاً «۵ سؤال»، و --tint را با card.style.setProperty("--tint", topic.color) بگیرد. در CSS بنویس .topic { border-top: 10px solid var(--tint); } و .topic small { opacity: 0.75; }. آخر اسکریپت بهجای showQuestion()، تابع showHome() را صدا کن.
<!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 .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;
}
</style>
</head>
<body>
<div class="app">
<header id="top">
<h1 id="app-name">کوییز من</h1>
<p id="tagline">موضوعت را انتخاب کن و خودت را بسنج</p>
</header>
<h2 id="pick-title">یک موضوع انتخاب کن</h2>
<div id="topics">
<button class="topic"><span class="icon">🔬</span><b>علوم</b></button>
<button class="topic"><span class="icon">🚀</span><b>فضا</b></button>
<button class="topic"><span class="icon">🔢</span><b>ریاضی</b></button>
</div>
<div id="status" class="status"><span id="counter"></span></div>
<h2 id="question"></h2>
<div id="options"></div>
<p id="msg"></p>
<button id="next" class="next hide">سؤال بعدی</button>
</div>
<script>
function num(n) {
return n.toLocaleString("fa-IR");
}
function byId(id) {
return document.getElementById(id);
}
const questions = [
{"text": "پایتخت ایران کدام شهر است؟", "options": ["اصفهان", "شیراز", "تهران", "تبریز"], "answer": 2, "level": "easy", "explain": "تهران پایتخت ایران است."},
{"text": "بزرگترین سیارهی منظومهی شمسی کدام است؟", "options": ["زحل", "نپتون", "مشتری", "زمین"], "answer": 2, "level": "easy", "explain": "مشتری از همهی سیارهها بزرگتر است."},
{"text": "حاصل ۷ × ۸ چند است؟", "options": ["۴۸", "۵۴", "۵۶", "۶۳"], "answer": 2, "level": "easy", "explain": "۷ × ۸ میشود ۵۶."},
];
let index = 0;
function showQuestion() {
const q = questions[index];
byId("counter").textContent = "سؤال " + num(index + 1) + " از " + num(questions.length);
byId("question").textContent = q.text;
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);
}
}
function choose(chosen) {
const q = questions[index];
const msg = byId("msg");
const buttons = document.querySelectorAll(".option");
buttons[q.answer].classList.add("right");
if (chosen === q.answer) {
msg.textContent = "آفرین! " + q.explain;
} else {
msg.textContent = "این یکی نشد. " + q.explain;
buttons[chosen].classList.add("wrong");
}
for (const b of buttons) {
b.disabled = true;
}
byId("next").classList.remove("hide");
}
function nextQuestion() {
index++;
if (index >= questions.length) {
showEnd();
} else {
showQuestion();
}
}
function showEnd() {
byId("question").textContent = "تمام شد!";
byId("options").replaceChildren();
byId("msg").textContent = "";
byId("next").classList.add("hide");
}
byId("next").addEventListener("click", nextQuestion);
showQuestion();
</script>
</body>
</html>
راهنما و جواب این چالش · برگشت به درس جلسهی 7: یک دور کامل بازی · چالش بعدی: Shuffle
بیشتر بخوان: حلقه for در پایتون: آموزش کامل با range و مثال