○ Control systems · state machines
Traffic-light state machine
Model a traffic light as states, transitions and timers, with a safe default.
- Level 3 · Innovator
- 7 tests
- 🐞 debug variant
- technically checked
Your task
A finite-state machine is a set of states plus rules for moving between them. Our light cycles green → amber → red → green, and each state lasts a fixed number of ticks: green 4, amber 1, red 3.
Write:
- nextLight(state): the state after
state. Any unknown state must return "red": when a controller is confused, the safe choice is to stop traffic. - simulate(ticks): an array with the state at each tick, starting in "green" at tick 0.
Hints (0 of 3 shown)
What gets checked
- nextLight("green") → "amber"
- nextLight("red") → "green"
- simulate(9) → ["green", "green", "green", "green", "amber", "red", "red",…
Plus 4 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.
Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments
Press Run to see what your program prints.
Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.