Skip to content

Only last configured elevator behaves #111

@ghost

Description

With the following code below:

{
    init: function(elevators, floors) {
        for (var f = 0; f < floors.length; f++) {
            var floor = floors[f];
            floor.on("up_button_pressed", function() {
               // Maybe tell an elevator to go to this floor?
                for (var e = 0; e < elevators.length; e++) {
                   var elevator = elevators[e];
                   if (elevator.destinationDirection() == "stopped"){
                       console.log("selected elevator:" + e);
                       elevator.goToFloor(floor.floorNum());
                       return
                   }else if (e == elevators.length - 1){
                    var random = Math.floor(Math.random() * elevators.length);
                    console.log("Random FLoor: " + random);
                    elevators[random].goToFloor(floor.floorNum());
                }
            }
        }); 
        
        floor.on("down_button_pressed", function() {
            // Maybe tell an elevator to go to this floor?
            for (var e = 0; e < elevators.length; e++) {
                var elevator = elevators[e];
                if (elevator.destinationDirection() == "stopped"){
                    console.log("selected elevator:" + e);
                    elevator.goToFloor(floor.floorNum());
                    return
                }else if (e == elevators.length - 1){
                    var random = Math.floor(Math.random() * elevators.length);
                    console.log("Random FLoor: " + random);
                    elevators[random].goToFloor(floor.floorNum());
                }
                
            }
        });
    }
    
    for (var e = 0; e < elevators.length; e++) {
        var elevator = elevators[e];
        elevator.on("floor_button_pressed", function(floorNum){
            elevator.goToFloor(floorNum);
        });
        
        elevator.on("idle", function(){
            elevator.goToFloor(0);
        });
        
        elevator.on("stopped_at_floor", function(){
            console.log("Stopped at floor");
            elevator.goToFloor(0);
        });
    }
    
},
update: function(dt, elevators, floors) {
    // We normally don't need to do anything here
}
  }

I'm just learning everything right now - but to my understanding this should configure each of the elevators. However, it only seems to ever work with the final elevator in the array. Any elevators configured first will go straight to the top floor and stay there.

Is there something I am not understanding about javascript or the setup?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions