magwo / elevatorsaga

The elevator programming game!
http://play.elevatorsaga.com
MIT License
2.43k stars 319 forks source link

Only last configured elevator behaves #111

Open ghost opened 6 years ago

ghost commented 6 years ago

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?

megCanicalKb commented 6 years ago

use let instead of var

nivoset commented 5 years ago

actually i think its the code up above it in the hall buttons presses, you use the elevator variable, that will only be the last elevator from the loop below setting it. changing it to a let would just make elevator above undefined and throw an exception