Use this panel while solving. Syntax is filtered by current checkpoint.
Level 1 Syntax
Starter Structure
{
init: function(elevators, floors) {
var elevator = elevators[0];
},
update: function(dt, elevators, floors) {
}
}
init(...): Runs once at challenge start. Set up your rules and event handlers here.
update(dt,...): Runs repeatedly while simulation is active. Use for advanced logic.
elevators: Array of elevator objects you can control.
floors: Array of floor objects that emit up/down button events.
var elevator = elevators[0]: Stores the first elevator in a variable for easy use.
Basic Elevator Events
elevator.on("idle", function() {
elevator.goToFloor(0);
});
elevator.on("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
});
.on(event, callback): Listens for an event and runs your function when it happens.
"idle": Fires when the elevator has no more queued destinations.
"floor_button_pressed": Fires when a passenger presses a floor inside the elevator.
floorNum: The exact floor number requested by the passenger.
goToFloor(n): Adds floor n to the elevator’s destination queue.
Basic Floor Events
floors[0].on("up_button_pressed", function() {
elevator.goToFloor(0);
});
floors[4].on("down_button_pressed", function() {
elevator.goToFloor(4);
});
floors[i]: Accesses the floor object at index i.
"up_button_pressed": Someone on that floor wants to go up.
"down_button_pressed": Someone on that floor wants to go down.
- In Level 1, writing one line per floor keeps syntax simple and explicit.
Chaining Two Events in One Listener
floors[1].on("up_button_pressed down_button_pressed", function() {
elevator.goToFloor(1);
});
- Put both event names in one string, separated by a space.
- The callback runs when either up or down is pressed on that floor.
- Useful for middle floors where both directions can be requested.
Level 1 Route Pattern (No Loops)
elevator.on("idle", function() {
elevator.goToFloor(0);
elevator.goToFloor(1);
elevator.goToFloor(2);
elevator.goToFloor(3);
elevator.goToFloor(4);
});
- This queues a full route when the elevator becomes idle.
- Each
goToFloor(...) call is added in order to the queue.
- Good for beginner challenges where floors are known ahead of time.
Level 2+ Loop Pattern
for (var i = 0; i < floors.length; i++) {
elevator.goToFloor(i);
}
for (...): Repeats code while a condition is true.
i = 0: Start counter at 0.
i < floors.length: Keep going until the last floor index.
i++: Increase i by 1 each loop step.
- Use this after loops are taught (Level 2+).
Multiple Elevators
var e0 = elevators[0];
var e1 = elevators[1];
e0.on("idle", function() { e0.goToFloor(0); });
e1.on("idle", function() { e1.goToFloor(4); });
- Access elevators by index when you have more than one car.
- Give each elevator an idle fallback so both stay active.
Array Iteration (Level 3+)
elevators.forEach(function(e) {
e.on("floor_button_pressed", function(floorNum) {
e.goToFloor(floorNum);
});
});
forEach runs once for each item in an array.
- Useful for attaching similar behavior to all elevators.
Passing Floor Optimization
elevator.on("passing_floor", function(floorNum, direction) {
if (elevator.loadFactor() < 0.8) {
elevator.goToFloor(floorNum, true);
}
});
passing_floor fires while moving between floors.
goToFloor(n, true) inserts urgent stops at front of queue.
loadFactor() helps avoid stopping when nearly full.