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

جواب «Step 1: Start and the First Question» چالش 1 از جلسه‌ی 40

سؤال

صفحه‌ی شروع آماده است، ولی دکمه‌ی Start کاری نمی‌کند.
1. در App یک state به اسم screen بساز که اول "start" است. دکمه‌ی #start آن را "quiz" کند (تابع را با prop به اسم onStart به Start بده).
2. کامپوننت Quiz را بنویس که یک prop به اسم question می‌گیرد و این را برمی‌گرداند: یک div با id برابر quiz و کلاس card، داخلش یک h2 با id برابر question (متن سؤال) و برای هر گزینه یک دکمه با کلاس option و id برابر opt0، opt1، opt2.
3. وقتی صفحه "quiz" است، App به‌جای صفحه‌ی شروع، Quiz را با اولین سؤال نشان بدهد.

راهنما

دکمه‌ها: {question.options.map((option, i) => (<button key={option} id={"opt" + i} className="option">{option}</button>))}. در App: <Quiz question={QUESTIONS[0]} />

یک جواب درست

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.card { max-width: 260px; margin: 12px auto; padding: 16px; border: 2px solid #334155; border-radius: 16px; background: #f8fafc; }
.row { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.option { min-width: 90px; }
.right { background: #bbf7d0; }
.wrong { background: #fecaca; }
</style>

<div id="root" dir="ltr"></div>

<script type="text/jsx">
import { useState } from "react";
import { createRoot } from "react-dom/client";

const QUESTIONS = [
  { id: 1, text: "What colour is the sky?", options: ["Green", "Blue", "Red"], answer: 1 },
  { id: 2, text: "How many legs does a spider have?", options: ["Eight", "Six", "Ten"], answer: 0 },
  { id: 3, text: "Which planet do we live on?", options: ["Mars", "Venus", "Earth"], answer: 2 },
];

function Start({ onStart }) {
  return (
    <div id="start-screen" className="card">
      <h1>Sky Quiz</h1>
      <p>{QUESTIONS.length} questions</p>
      <button id="start" onClick={onStart}>Start</button>
    </div>
  );
}

function Quiz({ question }) {
  return (
    <div id="quiz" className="card">
      <h2 id="question">{question.text}</h2>
      <div className="row">
        {question.options.map((option, i) => (
          <button key={option} id={"opt" + i} className="option">{option}</button>
        ))}
      </div>
    </div>
  );
}

function App() {
  const [screen, setScreen] = useState("start");

  if (screen === "start") {
    return <Start onStart={() => setScreen("quiz")} />;
  }
  return <Quiz question={QUESTIONS[0]} />;
}

createRoot(document.getElementById("root")).render(<App />);
</script>

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