جواب «Step 4: Right or Wrong?» چالش 4 از جلسهی 40
سؤال
الان بعد از هر کلیک سؤال فوری عوض میشود و بازیکن نمیفهمد درست گفته یا نه. قدم آخر:
1. در App یک state به اسم picked بساز که اول null است. pick(i) دیگر سؤال را عوض نکند: فقط picked را i کند و اگر جواب درست بود امتیاز بدهد.
2. Quiz دو prop تازه بگیرد: picked و onNext. وقتی picked دیگر null نیست:
- همهی دکمههای گزینه disabled شوند.
- گزینهی درست کلاس right بگیرد (کنار option).
- اگر بازیکن گزینهی غلطی زده، همان گزینه کلاس wrong بگیرد.
- یک دکمه با id برابر next و متن Next دیده شود.
3. تابع next را در App بنویس: picked را null کند و به سؤال بعد برود، یا اگر سؤالی نمانده به صفحهی نتیجه.
4. again هم picked را null کند.
راهنما
داخل Quiz یک تابع کمکی بنویس: function look(i) { if (picked === null) { return "option"; } if (i === question.answer) { return "option right"; } if (i === picked) { return "option wrong"; } return "option"; } و به دکمه بده: className={look(i)} disabled={picked !== null}
یک جواب درست
<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, number, total, score, picked, onPick, onNext }) {
function look(i) {
if (picked === null) {
return "option";
}
if (i === question.answer) {
return "option right";
}
if (i === picked) {
return "option wrong";
}
return "option";
}
return (
<div id="quiz" className="card">
<p id="progress">Question {number} of {total}</p>
<h2 id="question">{question.text}</h2>
<div className="row">
{question.options.map((option, i) => (
<button key={option} id={"opt" + i} className={look(i)} disabled={picked !== null} onClick={() => onPick(i)}>{option}</button>
))}
</div>
<p id="score">Score: {score}</p>
{picked !== null && <button id="next" onClick={onNext}>Next</button>}
</div>
);
}
function Result({ score, total, onAgain }) {
return (
<div id="result-screen" className="card">
<h2 id="result">You got {score} of {total}</h2>
<p id="message">{score === total ? "Great job!" : "Nice try!"}</p>
<button id="again" onClick={onAgain}>Play again</button>
</div>
);
}
function App() {
const [screen, setScreen] = useState("start");
const [index, setIndex] = useState(0);
const [score, setScore] = useState(0);
const [picked, setPicked] = useState(null);
function pick(i) {
setPicked(i);
if (i === QUESTIONS[index].answer) {
setScore(score + 1);
}
}
function next() {
setPicked(null);
if (index + 1 < QUESTIONS.length) {
setIndex(index + 1);
} else {
setScreen("result");
}
}
function again() {
setIndex(0);
setScore(0);
setPicked(null);
setScreen("start");
}
if (screen === "start") {
return <Start onStart={() => setScreen("quiz")} />;
}
if (screen === "result") {
return <Result score={score} total={QUESTIONS.length} onAgain={again} />;
}
return <Quiz question={QUESTIONS[index]} number={index + 1} total={QUESTIONS.length} score={score} picked={picked} onPick={pick} onNext={next} />;
}
createRoot(document.getElementById("root")).render(<App />);
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن