Skip to content

On Your Own: Flashcards

The Course
•
25 min study

This is the course's final check: a different app, from a brief, with no walkthrough. Plan before you code. Sketch the components on paper and decide where each piece of state lives. Then build it one small, working step at a time.

Treat the suggested time as a first attempt, not a deadline. If the whole brief is too much for one sitting, first get one complete interaction working, then return for validation and the remaining cases. Before opening the solution, write down what works, what fails, and the next change you would try.

The brief

A flashcard deck for studying.

The quiz

  • Shows Card 1 of 2 and the current card's question.
  • Show answer reveals the answer, with two buttons: I got it and I missed it. Either one moves to the next card with its answer hidden. After the last card, it goes back to the first.
  • A score line: No answers yet at the start, then for example 1 of 2 right (50%).

Adding cards

  • A form with Question and Answer fields and an Add card button.
  • Both fields are required. If either is blank, show Write both a question and an answer.
  • A new card joins the end of the deck, and 2 cards in the deck above the quiz updates.

Structure

  • At least three components in separate files: the app, the quiz and the form.
  • Put the deck's logic in plain functions in src/deck.js, like tasks.js in Study Tasks.

Acceptance checks:

  • Answer both starter cards, one right and one wrong: 1 of 2 right (50%), and the quiz is back on Card 1 of 2.
  • After those two answers, adding a card makes 3 cards in the deck and Card 1 of 3. Adding must keep the current question and its answer visibility; it must not jump to another card. The new card appears at the end of the deck.
  • No errors or warnings in the console.
Your workspace React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); // Build the flashcard deck here. Plain functions for the deck can go in // src/deck.js, like tasks.js in Study Tasks. const starterCards = [ { id: 1, question: "What does a component return?", answer: "JSX" }, { id: 2, question: "Which hook gives a component memory?", answer: "useState" }, ]; export default function App() { return ( <main> <h1>Flashcards</h1> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 1rem; margin: 1rem 0; } label { display: block; margin: 0.5rem 0; } input, button { font: inherit; } .error { color: #b42318; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: where does the state go?

The cards are needed by the quiz and changed by the form, so they live in App. Which card is showing, whether its answer is visible, and the score are only needed by the quiz, so they can live in the quiz component.

Hint 2: going round

Keep the index of the current card, and wrap it into the current deck's range when advancing. Use the index directly when displaying the card. Taking a lifetime answer count modulo a growing deck size can jump to a different card when a card is added.

Show one solution
One solution React · 6 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; export default function AddCardForm({ onAdd }) { const [question, setQuestion] = useState(""); const [answer, setAnswer] = useState(""); const [error, setError] = useState(""); function handleSubmit(event) { event.preventDefault(); if (question.trim() === "" || answer.trim() === "") { setError("Write both a question and an answer."); return; } onAdd(question.trim(), answer.trim()); setQuestion(""); setAnswer(""); setError(""); } return ( <form onSubmit={handleSubmit}> <h2>Add a card</h2> <label> Question <input value={question} onChange={(e) => setQuestion(e.target.value)} /> </label> <label> Answer <input value={answer} onChange={(e) => setAnswer(e.target.value)} /> </label> {error && <p className="error">{error}</p>} <button type="submit">Add card</button> </form> ); } import { useState } from "react"; import AddCardForm from "./AddCardForm.jsx"; import Quiz from "./Quiz.jsx"; import { addCard } from "./deck.js"; const starterCards = [ { id: 1, question: "What does a component return?", answer: "JSX" }, { id: 2, question: "Which hook gives a component memory?", answer: "useState" }, ]; export default function App() { const [cards, setCards] = useState(starterCards); return ( <main> <h1>Flashcards</h1> <p className="muted">{cards.length} cards in the deck</p> <Quiz cards={cards} /> <AddCardForm onAdd={(question, answer) => setCards(addCard(cards, question, answer))} /> </main> ); } export function addCard(cards, question, answer) { return [...cards, { id: Date.now(), question, answer }]; } export function scoreText(right, seen) { if (seen === 0) return "No answers yet"; return `${right} of ${seen} right (${Math.round((right / seen) * 100)}%)`; } import { useState } from "react"; import { scoreText } from "./deck.js"; export default function Quiz({ cards }) { const [index, setIndex] = useState(0); const [showAnswer, setShowAnswer] = useState(false); const [right, setRight] = useState(0); const [seen, setSeen] = useState(0); const card = cards[index]; function next(gotItRight) { if (gotItRight) setRight(right + 1); setSeen(seen + 1); setShowAnswer(false); setIndex((current) => (current + 1) % cards.length); } return ( <section className="card"> <p className="muted">Card {index + 1} of {cards.length}</p> <h2>{card.question}</h2> {showAnswer ? ( <> <p><strong>{card.answer}</strong></p> <button onClick={() => next(true)}>I got it</button>{" "} <button onClick={() => next(false)}>I missed it</button> </> ) : ( <button onClick={() => setShowAnswer(true)}>Show answer</button> )} <p>{scoreText(right, seen)}</p> </section> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 1rem; margin: 1rem 0; } label { display: block; margin: 0.5rem 0; } input, button { font: inherit; } .error { color: #b42318; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Your structure can differ. What matters is that each piece of state has one home, that nothing is changed in place, and that the acceptance checks pass.

What next

If you built this, you can build small React apps on your own. Good next steps:

  • Download your flashcards and add tests and localStorage, as you did for Study Tasks.
  • Read the Learn React section of react.dev. It covers useRef, useReducer and context, which build on everything here.
  • React Intermediate in 7 Days picks up from here: reducers, context, refs, custom hooks, and keeping larger apps fast and tested.
  • For a framework path next, Next.js beginner introduces routing, server components, data loading and server actions.

Was this article helpful?

Your response is saved on this device.