Questions as Data چالش 1 از جلسهی 6، متوسط
متن گزینهها را از چهار دکمه پاک کن (دکمهها خالی میشوند) و const answer و questionText را بردار.
تابع byId(id) را بساز. آرایهی questions را با سه سؤال بساز: سؤال اول همان پایتخت ایران است و answer آن ۲. دو سؤال دیگر را از خودت بنویس (قالب کارت ۳). بعد let index = 0; و تابع showQuestion() را بساز که متن سؤالِ شمارهی index را در #question و گزینهها را در دکمهها بریزد. در choose بهجای answer از questions[index].answer استفاده کن. آخر اسکریپت showQuestion() را صدا کن.
<!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; }
</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>
<h2 id="question"></h2>
<div id="options">
<button class="option" onclick="choose(0)">اصفهان</button>
<button class="option" onclick="choose(1)">شیراز</button>
<button class="option" onclick="choose(2)">تهران</button>
<button class="option" onclick="choose(3)">تبریز</button>
</div>
<p id="msg"></p>
</div>
<script>
const questionText = "پایتخت ایران کدام شهر است؟";
document.getElementById("question").textContent = questionText;
const answer = 2;
function choose(chosen) {
const msg = document.getElementById("msg");
const buttons = document.querySelectorAll(".option");
buttons[answer].classList.add("right");
if (chosen === answer) {
msg.textContent = "آفرین!";
} else {
msg.textContent = "این یکی نشد.";
buttons[chosen].classList.add("wrong");
}
for (const b of buttons) {
b.disabled = true;
}
}
</script>
</body>
</html>
راهنما و جواب این چالش · برگشت به درس جلسهی 6: قالب سؤال: آرایهی شیءها · چالش بعدی: Buttons from Code
بیشتر بخوان: حلقه for در پایتون: آموزش کامل با range و مثال