0

I want to do this without writing the code X times. How you do that in JavaScript ? I am from Java.

 floors[0].on("up_button_pressed", function() { 
 elevator.goToFloor(floors[0].floorNum());
 } );
 floors[0].on("down_button_pressed", function() { 
 elevator.goToFloor(floors[0].floorNum());
 } );
 floors[1].on("up_button_pressed", function() { 
 elevator.goToFloor(floors[1].floorNum());
 } );
 floors[1].on("down_button_pressed", function() { 
 elevator.goToFloor(floors[1].floorNum());
 } );
 floors[2].on("up_button_pressed", function() { 
 elevator.goToFloor(floors[2].floorNum());
 } );
 floors[2].on("down_button_pressed", function() { 
 elevator.goToFloor(floors[2].floorNum());
 } ); 
 floors[3].on("up_button_pressed", function() { 
 elevator.goToFloor(floors[3].floorNum());
 } );
 floors[3].on("down_button_pressed", function() { 
 elevator.goToFloor(floors[3].floorNum());
 } ); 
 floors[4].on("up_button_pressed", function() { 
 elevator.goToFloor(floors[4].floorNum());
 } );
 floors[4].on("down_button_pressed", function() { 
 elevator.goToFloor(floors[4].floorNum());
 } );
asked Jan 25, 2015 at 11:19
2
  • Why does every individual floor have its own on event? There should be one event listener and the event should contain the information which button was pressed on which floor. Then the elevator can go to that floor. Commented Jan 25, 2015 at 11:23
  • it s a game : play.elevatorsaga.com/#challenge=3 I don t control the settings :) Commented Jan 25, 2015 at 11:59

3 Answers 3

1

First of all, in

floors[0].on("up_button_pressed", function () { 
 elevator.goToFloor(floors[0].floorNum());
});

the floors[0] inside the function is always the same object that the one that is triggering the event, so I would change it to use this:

floors[0].on("up_button_pressed", function () { 
 elevator.goToFloor(this.floorNum());
});

and then, just do it for all floors with forEach:

floors.forEach(function (floor) {
 floor.on("up_button_pressed", function () {
 elevator.goToFloor(this.floorNum());
 });
 // same for "down_button_pressed" here
});

Since handler functions do not need to access outer variable floor and use this, you can even define them out of loop and use them by name, if you wished:

function onFloorUp() {
 elevator.goToFloor(this.floorNum());
}
// same for onFloorUp
floors.forEach(function (floor) {
 floor.on("up_button_pressed", onFloorUp);
 // same for "down_button_pressed" here
});
answered Jan 25, 2015 at 11:40
Sign up to request clarification or add additional context in comments.

Comments

1

You can just make use of Array.forEach and refactor your code to the below

floors.forEach(function(floor){
 floor.on('up_button_pressed,down_button_pressed', function(){
 elevator.goToFloor(floor.floorNum());
 });
});

You're not doing anything specific when when up_button_pressed or down_button_pressed is triggered, so just combine those events using a comma.

answered Jan 25, 2015 at 11:23

Comments

1

here we go... first you take one of your function that you need to be iterated.

floors[0].on("up_button_pressed", function() { 
 elevator.goToFloor(floors[0].floorNum());
 } );

then you transform that code so that code can work in iteration (for example for loop)

for(var i=0; i<x; i++){
floors[i].on("up_button_pressed", function(i) { 
 elevator.goToFloor(floors[i].floorNum());
}, i ); }

I do that on casperjs, but I think that will be work on regular javascript too...(because casperjs is javascript too)

answered Jan 25, 2015 at 11:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.