Write a dispatch algorithm in JavaScript. Watch it succeed—or strand passengers on floor 7. A classic coordination puzzle, now with real-time feedback and nowhere to hide.
Learn how to write algorithms that control elevators to transport passengers efficiently.
Your algorithm controls elevators to pick up waiting passengers and deliver them to their destinations. The goal is to minimize wait times and maximize throughput.
{ init: function(elevators, floors, store) { // Called once when simulation starts // Set up event handlers and initial state elevators.forEach(elevator => { elevator.on('idle', function() { // Elevator has no destinations - what should it do? }); elevator.on('floorButtonPressed', function(floorNum) { // A passenger inside pressed a floor button elevator.goToFloor(floorNum); }); }); floors.forEach(floor => { floor.on('callButtonPressed', function(direction) { // Someone pressed the call button on this floor }); }); }, update: function(dt, elevators, floors, store) { // Called every tick - use for periodic logic } }
The store is persistent key-value storage that persists across ticks. Use it to coordinate between elevators and track global state.
isFull() before picking up more passengersgoToFloor(n, true) for sorted insertionTransported: Total passengers delivered to their destination.
Average Wait: Mean time from spawn to pickup (lower is better).
Max Wait: Longest any passenger waited (minimize this!).
Waiting: Current passengers waiting at floors.
Riding: Current passengers in elevators.