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

Your Questions in the Game چالش 4 از جلسه‌ی 11، سخت

let mine = salam.load("quiz.mine", []); را بساز. کلید mine («سؤال‌های خودم» / My questions) را به WORDS اضافه کن.
در addPasted بعد از قبول شدن، سؤال‌ها را به mine اضافه و با salam.save("quiz.mine", mine) ذخیره کن و #paste را خالی کن.
topicsNow() را عوض کن: اگر mine خالی نبود، اول فهرست یک موضوع بیاید: { id: "mine", title: t("mine"), icon: "✨", color: "#ffc531", questions: mine }.
تبریک! با این قدم اپ کامل است. 🎉

<!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;
}
:root[data-theme="dark"] {
  --bg: #17112b;
  --card: #241b40;
  --text: #f6f1ff;
  --line: #0b0716;
  --main: #8f6bff;
}
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; }
.toolbar { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-block-end: 10px; }
.tool {
  min-height: 42px;
  padding: 6px 14px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: inherit;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}
#open-maker { margin-block-start: 18px; }
#maker { display: grid; gap: 12px; margin-block-start: 16px; }
#maker.hide { display: none; }
input, textarea {
  width: 100%;
  max-width: 100%;
  padding: 12px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: inherit;
  font: inherit;
  font-size: 16px;
}
#prompt-out { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
textarea { min-height: 150px; }
#maker-msg { font-weight: 800; }
#maker-msg.is-bad { color: #e5484d; }
#maker-msg.is-good { color: #16a56b; }
</style>
</head>
<body>
<div class="app">
<div class="toolbar"><button id="theme" class="tool">تاریک</button><button id="lang" class="tool">English</button></div>
<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>
<button id="open-maker" class="next">✨ ساخت سؤال با AI</button>
</section>
<section id="maker" class="hide">
  <h2 id="maker-title">✨ ساخت سؤال با AI</h2>
  <p id="maker-help">موضوع را بنویس، متن را به هوش مصنوعی بده و جوابش را بچسبان.</p>
  <input id="ai-topic" placeholder="موضوع، مثلاً: منظومه‌ی شمسی">
  <button id="make-prompt" class="next">ساختن متن برای AI</button>
  <pre id="prompt-out"></pre>
  <textarea id="paste" dir="ltr" placeholder="[ { ... } ]"></textarea>
  <button id="add" class="next">اضافه کردن به اپ</button>
  <p id="maker-msg"></p>
  <button id="maker-home" class="tool">خانه</button>
</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(lang === "fa" ? "fa-IR" : "en-US");
}

const WORDS = {
  fa: { name: "کوییز من", tagline: "موضوعت را انتخاب کن و خودت را بسنج", pick: "یک موضوع انتخاب کن", other: "English", dark: "تاریک", light: "روشن",
    maker: "✨ ساخت سؤال با AI", makerHelp: "موضوع را بنویس، متن را به هوش مصنوعی بده و جوابش را بچسبان.", topicHint: "موضوع، مثلاً: منظومه‌ی شمسی", makePrompt: "ساختن متن برای AI", general: "دانستنی‌های عمومی", notList: "باید یک فهرست از سؤال‌ها باشد که با [ شروع می‌شود.", noText: "متن ندارد.", noOptions: "باید چهار گزینه‌ی متنی داشته باشد.", noAnswer: "answer باید عددی از 0 تا 3 باشد.", noLevel: "level باید easy یا medium یا hard باشد.", add: "اضافه کردن به اپ", added: "سؤال اضافه شد.", badJson: "این متن JSON درستی نیست. آن را کامل کپی کن و دوباره بچسبان.",
    questions: "سؤال", question: "سؤال", of: "از", good: "آفرین! ", miss: "این یکی نشد. ", timeUp: "وقت تمام شد. ", next: "سؤال بعدی", result: "دیدن نتیجه",
    best: "بهترین امتیاز تو", record: "رکورد تازه زدی! 🏆", right: "جواب درست از", again: "دوباره بازی کن", home: "خانه", answer: "جواب" },
  en: { name: "My Quiz", tagline: "Pick a topic and test yourself", pick: "Pick a topic", other: "فارسی", dark: "Dark", light: "Light",
    maker: "✨ Make questions with AI", makerHelp: "Write a topic, give the text to an AI and paste its reply.", topicHint: "A topic, like: the solar system", makePrompt: "Make the text for AI", general: "general knowledge", notList: "It must be a list of questions that starts with [.", noText: "has no text.", noOptions: "must have four text options.", noAnswer: "answer must be a number from 0 to 3.", noLevel: "level must be easy, medium or hard.", add: "Add to the app", added: "questions added.", badJson: "This is not valid JSON. Copy all of it and paste it again.",
    questions: "questions", question: "Question", of: "of", good: "Well done! ", miss: "Not this time. ", timeUp: "Time is up. ", next: "Next question", result: "See the result",
    best: "Your best score", record: "A new record! 🏆", right: "right out of", again: "Play again", home: "Home", answer: "Answer" },
};
let lang = salam.load("quiz.lang", "fa");

function t(key) {
  return WORDS[lang][key];
}

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

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": "۹ × ۹ می‌شود ۸۱."},
  ]},
];
const TOPICS_EN = [
  {"id": "science", "title": "Science", "questions": [
    {"text": "At normal pressure, at how many degrees does water boil?", "options": ["50", "80", "100", "150"], "answer": 2, "level": "easy", "explain": "At normal pressure water boils at 100 degrees."},
    {"text": "Which gas from the air do plants use to make food?", "options": ["Oxygen", "Carbon dioxide", "Nitrogen", "Helium"], "answer": 1, "level": "easy", "explain": "Plants take in carbon dioxide and give back oxygen."},
    {"text": "What is the largest organ of the human body?", "options": ["Heart", "Liver", "Skin", "Brain"], "answer": 2, "level": "medium", "explain": "Skin covers the whole body."},
    {"text": "Which metal is liquid at room temperature?", "options": ["Iron", "Mercury", "Copper", "Gold"], "answer": 1, "level": "medium", "explain": "Mercury is liquid at room temperature."},
    {"text": "About how many minutes does sunlight take to reach Earth?", "options": ["1", "8", "30", "60"], "answer": 1, "level": "hard", "explain": "Sunlight travels for about 8 minutes."},
  ]},
  {"id": "space", "title": "Space", "questions": [
    {"text": "Which planet is closest to the Sun?", "options": ["Venus", "Mercury", "Mars", "Earth"], "answer": 1, "level": "easy", "explain": "Mercury is the closest to the Sun."},
    {"text": "Which is the largest planet of the solar system?", "options": ["Saturn", "Neptune", "Jupiter", "Earth"], "answer": 2, "level": "easy", "explain": "Jupiter is bigger than every other planet."},
    {"text": "About how many days does Earth take to go round the Sun?", "options": ["30", "100", "245", "365"], "answer": 3, "level": "easy", "explain": "One trip takes about 365 days."},
    {"text": "Which planet is called the red planet?", "options": ["Mars", "Jupiter", "Mercury", "Venus"], "answer": 0, "level": "easy", "explain": "Mars has rusty, red soil."},
    {"text": "Which planet has the famous bright rings?", "options": ["Earth", "Mercury", "Saturn", "Mars"], "answer": 2, "level": "medium", "explain": "Saturn's rings are made of ice and rock."},
  ]},
  {"id": "math", "title": "Math", "questions": [
    {"text": "What is 7 × 8?", "options": ["48", "54", "56", "63"], "answer": 2, "level": "easy", "explain": "7 × 8 is 56."},
    {"text": "How many degrees do the angles of a triangle add up to?", "options": ["90", "180", "270", "360"], "answer": 1, "level": "easy", "explain": "The angles of every triangle add up to 180."},
    {"text": "What is the first prime number after 10?", "options": ["11", "12", "13", "15"], "answer": 0, "level": "medium", "explain": "11 divides only by 1 and itself."},
    {"text": "What is 25% of 80?", "options": ["10", "15", "20", "25"], "answer": 2, "level": "medium", "explain": "A quarter of 80 is 20."},
    {"text": "What is the square root of 81?", "options": ["7", "8", "9", "11"], "answer": 2, "level": "medium", "explain": "9 × 9 is 81."},
  ]},
];
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 = t("question") + " " + num(index + 1) + " " + t("of") + " " + 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 = t("good") + q.explain;
  } else {
    lives--;
    missed.push(q);
    byId("lives").textContent = "❤️".repeat(lives) || "💔";
    msg.textContent = t(chosen < 0 ? "timeUp" : "miss") + 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 = t(last ? "result" : "next");
  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 ? t("record") : t("best") + ": " + 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) + " " + t("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", "", t("answer") + ": " + 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 ? "🏆 " + t("best") + ": " + num(best) : "";
  const grid = byId("topics");
  grid.replaceChildren();
  for (const topic of topicsNow()) {
    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) + " " + t("questions")));
    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);
  }
}

function setTheme(theme) {
  document.documentElement.dataset.theme = theme;
  byId("theme").textContent = t(theme === "dark" ? "light" : "dark");
}

function toggleTheme() {
  const next = document.documentElement.dataset.theme === "dark" ? "light" : "dark";
  setTheme(next);
  salam.save("quiz.theme", next);
}

function applyWords() {
  byId("app-name").textContent = t("name");
  byId("tagline").textContent = t("tagline");
  byId("pick-title").textContent = t("pick");
  byId("lang").textContent = t("other");
  byId("next").textContent = t("next");
  byId("again").textContent = t("again");
  byId("to-home").textContent = t("home");
  byId("maker-home").textContent = t("home");
  byId("open-maker").textContent = t("maker");
  byId("maker-title").textContent = t("maker");
  byId("maker-help").textContent = t("makerHelp");
  byId("ai-topic").placeholder = t("topicHint");
  byId("make-prompt").textContent = t("makePrompt");
  byId("add").textContent = t("add");
  byId("theme").textContent = t(document.documentElement.dataset.theme === "dark" ? "light" : "dark");
}

function setLang(next) {
  lang = next;
  document.documentElement.lang = next;
  document.documentElement.dir = next === "fa" ? "rtl" : "ltr";
  document.title = t("name");
  applyWords();
}

function toggleLang() {
  setLang(lang === "fa" ? "en" : "fa");
  salam.save("quiz.lang", lang);
  showHome();
}

function topicsNow() {
  if (lang === "fa") {
    return TOPICS;
  }
  return TOPICS.map((topic, i) => ({ ...topic, ...TOPICS_EN[i] }));
}

function aiPrompt(topic) {
  if (lang === "en") {
    return "Make six multiple-choice questions in English about \"" + topic + "\". Reply with JSON only and no other text.\n"
      + "The reply must be an array and each item must have exactly this shape:\n"
      + '{"text":"The question","options":["One","Two","Three","Four"],"answer":0,"level":"easy","explain":"A short explanation"}\n'
      + "answer is the number of the right option, from 0 to 3. level is easy, medium or hard.";
  }
  return "برای موضوع «" + topic + "» شش سؤال چهارگزینه‌ای فارسی بساز. فقط JSON بده و هیچ متن دیگری ننویس.\n"
    + "خروجی یک آرایه باشد و هر عضو دقیقاً این شکل را داشته باشد:\n"
    + '{"text":"متن سؤال","options":["گزینه ۱","گزینه ۲","گزینه ۳","گزینه ۴"],"answer":0,"level":"easy","explain":"توضیح کوتاه"}\n'
    + "answer شماره‌ی گزینه‌ی درست از 0 تا 3 است. level یکی از easy و medium و hard است.";
}

function showMaker() {
  show("maker");
}

function makePrompt() {
  byId("prompt-out").textContent = aiPrompt(byId("ai-topic").value.trim() || t("general"));
}

function say(kind, text) {
  byId("maker-msg").className = kind;
  byId("maker-msg").textContent = text;
}

function addPasted() {
  let list;
  try {
    list = JSON.parse(byId("paste").value);
  } catch {
    say("is-bad", t("badJson"));
    return;
  }
  const problems = checkQuestions(list);
  if (problems.length) {
    say("is-bad", problems.slice(0, 3).join(" "));
    return;
  }
  say("is-good", num(list.length) + " " + t("added"));
}

function checkQuestions(list) {
  if (!Array.isArray(list) || list.length === 0) {
    return [t("notList")];
  }
  const problems = [];
  list.forEach((one, i) => {
    const name = t("question") + " " + num(i + 1) + ": ";
    if (!one || typeof one.text !== "string" || !one.text.trim()) {
      problems.push(name + t("noText"));
    }
    if (!one || !Array.isArray(one.options) || one.options.length !== 4 || one.options.some((o) => typeof o !== "string" || !o.trim())) {
      problems.push(name + t("noOptions"));
    }
    if (!one || !Number.isInteger(one.answer) || one.answer < 0 || one.answer > 3) {
      problems.push(name + t("noAnswer"));
    }
    if (!one || !["easy", "medium", "hard"].includes(one.level)) {
      problems.push(name + t("noLevel"));
    }
  });
  return problems;
}

byId("add").addEventListener("click", addPasted);
byId("make-prompt").addEventListener("click", makePrompt);
byId("maker-home").addEventListener("click", showHome);
byId("open-maker").addEventListener("click", showMaker);
setLang(lang);
byId("lang").addEventListener("click", toggleLang);
setTheme(salam.load("quiz.theme", "light"));
byId("theme").addEventListener("click", toggleTheme);
byId("to-home").addEventListener("click", showHome);
byId("again").addEventListener("click", () => startGame(topicNow));

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

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 11: هوش مصنوعی، دستیار تو

بیشتر بخوان: دیکشنری در پایتون: آموزش کامل کلید و مقدار با مثال