CLI Quiz or Habit Log
Build a small command-line program in Node — either a quiz that scores a player, or a habit log that records daily entries — that remembers state across runs.
Practices: Values, variables, and operators, Control flow, Functions and scope, Arrays and objects
Scenario
Pick one of two programs to build with Node, run from a terminal:
- A quiz: ask the player a series of questions, track how many they got right, and print a final score.
- A habit log: let the user log today’s entry for one or more habits (did you exercise? did you read?) and print a short summary of the last several days.
Both need the same underlying skills — reading input, deciding what to do with it, tracking state across a sequence of steps, and organizing that state in arrays and objects. Pick whichever sounds more interesting; this brief describes the outcome, not the implementation.
Requirements
Core
- The program runs from
node <yourfile>.jsand reads input from the terminal (Node’s built-inreadlinemodule, or a small prompt library, both work). - State — the questions and running score, or the habit entries — is held in arrays and/or plain objects, not a pile of separate variables.
- The program handles at least 3–5 distinct items (questions, or habits) without duplicating the same block of code for each one.
- Invalid input (an empty answer, a habit name that doesn’t exist) is handled without crashing the program.
- At the end, the program prints a clear summary: the final score, or the recent log.
Stretch
- Persist state to a JSON file with Node’s
fsmodule, so a habit log (or quiz high score) survives between runs. - Add a difficulty or category to each quiz question, and report a per-category breakdown.
- Let the user pick from a menu of actions (log an entry, view summary, exit) in a loop, rather than running once and exiting.
Reflect
- Which parts of your program are pure functions (same input always gives the same output, no side effects) versus functions that read input or print output? Could you test the pure ones without touching the terminal at all?
- If you added a second player or a second habit-tracking user, what would have to change about how you’re storing state?
Suggested milestones
- Hard-code one question (or one habit entry) and get the program printing something correct for it — no loop, no real input handling yet.
- Replace the hard-coded item with an array of items, and loop over it.
- Wire up real terminal input and track running state (score, or the day’s entries) as you go.
- Add the final summary output.
- Attempt one stretch requirement.
Acceptance checks
- Running the program end to end, with a mix of correct/incorrect quiz answers (or a mix of logged and skipped habits), produces a summary that matches what was entered.
- An empty or unexpected input at any prompt doesn’t crash the program — it’s handled and the program continues.
- The item data (questions, or habits) lives in one array or object structure that a new item can be added to without touching the control-flow logic elsewhere in the file.