Skip to content
dreamcode
dreamcode
Projects
Mission Control
Capstone projectJavaScript
Reward: +600 XP
PROJECT BRIEF

Mission Control

Mission control sends a list of commands. Write runMission(commands) that starts with { status: "ready", fuel: 0, altitude: 0 } and applies each command in order:

  • { type: "fuel", amount }: only while ready. Adds fuel, but the tank holds at most 100.
  • { type: "launch" }: only while ready and with at least 50 fuel. Status becomes "flying" and uses 50 fuel.
  • { type: "burn", amount }: only while flying and with enough fuel. Uses that much fuel and climbs 10 altitude per unit.
  • { type: "land" }: only while flying. Status becomes "landed" and altitude returns to 0.

Any command that breaks a rule, or has an unknown type, is rejected: the state stays the same and its index goes in a rejected list. Return the final state with rejected added.

Build it step by step
  1. Loop with commands.forEach((cmd, i) => ...) so you have each index.
  2. Use a switch on cmd.type with one case per command.
  3. Inside each case, check the rules first. If one fails, push i to rejected and stop.
  4. Only change the state when every rule passes.
Examples
runMission([{ type: "fuel", amount: 80 }, { type: "launch" }, { type: "burn", amount: 20 }, { type: "land" }])
{ status: "landed", fuel: 10, altitude: 0, rejected: [] }
runMission([{ type: "launch" }, { type: "fuel", amount: 60 }, { type: "launch" }])
{ status: "flying", fuel: 10, altitude: 0, rejected: [0] }
project.js
JAVASCRIPT
Saved as you type

Tests

0 of 5 passing
  • a clean flight
    runMission([{ type: "fuel", amount: 80 }, { type: "launch" }, { type: "burn", amount: 20 }, { type: "land" }])
    expected { status: "landed", fuel: 10, altitude: 0, rejected: [] }
  • launch without fuel
    runMission([{ type: "launch" }, { type: "fuel", amount: 60 }, { type: "launch" }])
    expected { status: "flying", fuel: 10, altitude: 0, rejected: [0] }
  • tank caps at 100
    runMission([{ type: "fuel", amount: 70 }, { type: "fuel", amount: 70 }])
    expected { status: "ready", fuel: 100, altitude: 0, rejected: [] }
  • unsafe burns and odd commands
    runMission([{ type: "fuel", amount: 55 }, { type: "launch" }, { type: "burn", amount: 9 }, { type: "fuel", amount: 5 }, { type: "dance" }, { type: "burn", amount: 3 }])
    expected { status: "flying", fuel: 2, altitude: 30, rejected: [2, 3, 4] }
  • no commands
    runMission([])
    expected { status: "ready", fuel: 0, altitude: 0, rejected: [] }
On the line
+600 XP
Pass all 5 tests to claim it.