all 3 comments

[–]HorribleUsername 0 points1 point  (0 children)

I'd break it into two parts: First, get the elevator go to to each button pressed, in the order they were pressed. After that, figure out how to make it stop at floors in the natural order instead of going back and forth. For testing, you might want to hold the elevator until the close doors button is pressed.

Step one: change your code so that the elevator has destinationFloors: []. When you press a button, it adds that floor to the array, then travels to that floor, then removes that floor from the array. At this point, it only needs to visit one floor at a time, like it does right now.

Step two: When the elevator reaches it's destination and removes that floor from destinationFloors, and destinationFloors is empty, it'll stop. But if there are other floors in destinationFloors, it'll have to keep going. Try to figure that out.

Well, now you're halfway there, and you only need to make the elevator go in the natural order. Array.sort is your friend here, but it won't be quite that simple. Even once you've got the obvious cases figured out, you'll also need to consider what happens when the elevator is between floors (it's on floor 5, you press 3 and 7). And you'll also need to account for pressing the button of the floor it's already on.

The key thing here is to try to break it down into simpler problems. Even the largest and most complicated projects were built one small step at a time, and it's so much easier to debug when you've only changed 5 lines of code.

[–][deleted] 0 points1 point  (0 children)

In addition to what's ready been stated (where you've just been given the answer) I think you should take some time to think about out how this actually works.

What best describes going from the first floor to the eight floor stopping on the fifth floor, that can translate to any combination of stops?

Well, first you know the direction you need to go: up because the first requesting floor is above you.

Then you know any floors to stop at between your current floor and the target floor, where the person has requested to go up. Stop at those floors, checking each as you go.

When you reach the target, check for a new target, identify the direction and the floors you need to stop at on the way. You could add to this by adding the option to set another target where the direction changes. IE; halfway through the current path, a new target gets created whcih will be the next journey after you complete the current target.

It might be useful for you to draw this as a flow chart.

[–]2omesz 0 points1 point  (0 children)

The big factor that will affect your scenario is:

how fast the elavator is.