Traffic light cycle with pedestrian sign and request button
Build the signal for a mid-block crosswalk. A traffic light for cars cycles through its phases forever, a pedestrian sign tells people on foot when they may cross, and a push button lets a pedestrian ask for the crossing sooner.
The starter code includes the phase config. The light starts at the first phase, and after the last phase it goes back to the first:
const PHASES = [
{ color: 'Green', durationMs: 5000 },
{ color: 'Yellow', durationMs: 2000 },
{ color: 'Red', durationMs: 4000 },
];
const REQUEST_LEAD_MS = 1000;
aria-hidden="true" and also show the state as text: Cars: Green, Cars: Yellow or Cars: Red.Pedestrians: Walk while the light is red and Pedestrians: Wait otherwise.Next change in Ns, where N is the number of whole seconds left in the current phase, rounded up (Math.ceil(msLeft / 1000)). It reads 5s right when green starts and 1s during its last second.disabled attribute or aria-disabled="true").Crossing requested until the light turns red, then hides it,REQUEST_LEAD_MS (1 second) after the press, if more than that is left. If 1 second or less is left, the timing doesn’t change.t = 0s Cars: Green Pedestrians: Wait Next change in 5s
t = 4.9s Cars: Green Next change in 1s
t = 5s Cars: Yellow Next change in 2s
t = 7s Cars: Red Pedestrians: Walk Next change in 4s
t = 11s Cars: Green Pedestrians: Wait Next change in 5s
With a request:
t = 1s press Crossing requested Next change in 1s (button disabled)
t = 2s Cars: Yellow Crossing requested Next change in 2s
t = 4s Cars: Red Pedestrians: Walk (message hidden)
t = 8s Cars: Green button enabled again
The tests replace Date, setTimeout, setInterval, requestAnimationFrame and performance.now with a fake clock, move time forward, press the button and read the screen like a user would. You don’t need test ids, class names or an exact HTML structure. They check:
Cars: {color}, Pedestrians: Walk / Pedestrians: Wait, Next change in Ns and Crossing requested,<button> (or role="button" element) whose text contains Request crossing, and whether it is disabled.Each check happens at least 100ms away from a phase change or a whole-second boundary, so updating the screen every 100ms or faster is enough. Capitalization and extra spacing don’t matter.