daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * The task is to implement an elevator driver. Elevator moves between floors (up & down).
  3.  * There are button for each floor in the elevator cabin (no buttons to open/close the doors).
  4.  * On each floor there are 2 buttons UP & DOWN (1 button for the highest floor DOWN and 1 for the lowest UP).
  5.  * The elevator should move according to the floors selected by users, but neither user should have priority and each user should be able to go to the desired floor (i.e. if there is 25 floors in the building, users on each floor should have the same priority; also there should be no possibility to stop the elevator from moving to users that are not inside of the cabin e.g. by moving between 2nd & 3rd floor endlessly).
  6.  *
  7.  * Implementation notes:
  8.  *
  9.  * - Start updating the template code from `Elevator.prototype = {` line
  10.  * - If elevator is not moving `getCurrentDirection` returns `DIRECTION_NONE`
  11.  * - `getCurrentFloor` called from `onBeforeFloor` returns the floor number that was before the floor for which `onBeforeFloor` is called (i.e. if the elevator goes up after the 2nd floor, and `onBeforeFloor` is called for the 3rd floor, `getCurrentFloor` will return 2)
  12.  * - `moveUp` & `moveDown` functions starts movement and are not discrete (i.e. if `moveUp` is called you should explicitly provide stop conditions and stop the elevator - it doesn't move the elevator up by one floor).
  13.  */
  14.  
  15.  
  16. const DIRECTION_DOWN = -1;
  17. const DIRECTION_NONE = 0;
  18. const DIRECTION_UP = 1;
  19.  
  20. function HardwareElevator() {}
  21.  
  22. HardwareElevator.prototype = {
  23.     moveUp: function() { console.log("up") },
  24.     moveDown: function() { console.log("down") },
  25.     stopAndOpenDoors: function() { console.log("stop and open") },
  26.     getCurrentFloor: function() {},
  27.     getCurrentDirection: function() {}
  28. };
  29.  
  30. function Elevator() {
  31.     this.hw = new HardwareElevator();
  32.     this.hw.on("doorsClosed", _bind(this.onDoorsClosed, this));
  33.     this.hw.on("beforeFloor", _bind(this.onBeforeFloor, this));
  34.     this.hw.on("floorButtonPressed", _bind(this.onFloorButtonPressed, this));
  35.     this.hw.on("cabinButtonPressed", _bind(this.onCabinButtonPressed, this));
  36.  
  37.     this.queue = {
  38.         top: [],
  39.         bottom: []
  40.     };
  41. }
  42.  
  43. Elevator.prototype = {
  44.     // your changes starting from here
  45.     addToQueue(floor, direction) {
  46.         const currentDirection = this.getCurrentDirection();
  47.         const currentFloor = this.getCurrentFloor();
  48.         const queue = this.queue[direction];
  49.  
  50.         if (currentFloor === floor) {
  51.             // open door on same floor
  52.             this.stopAndOpenDoors();
  53.             return;
  54.         }
  55.  
  56.         if (queue.indexOf(floor) !== -1) {
  57.             return;
  58.         }
  59.  
  60.         queue.push(floor).sort();
  61.  
  62.         if (direction === DIRECTION_UP) {
  63.             queue.reverse();
  64.         }
  65.  
  66.         this.queue[direction] = queue;
  67.     },
  68.  
  69.     getDirection() {
  70.         const currentDirection = this.getCurrentDirection();
  71.         const anotherDirection = this.getAnotherDirection(currentDirection);
  72.  
  73.         if (currentDirecction !== DIRECTION_NONE) {
  74.             if (this.queue[currentDirection].length) {
  75.                 return currentDirection;
  76.             }
  77.  
  78.             if (this.queue[anotherDirection].length) {
  79.                 return anotherDirection;
  80.             }
  81.         }
  82.  
  83.         return null;
  84.     },
  85.  
  86.     getAnotherDirection(direction) {
  87.         return [DIRECTION_UP, DIRECTION_DOWN].filter(d => d !== direction)[0];
  88.     },
  89.  
  90.     getNextFloor() {
  91.         const direction = this.getDirection();
  92.         const anotherDirection = this.getAnotherDirection(direction);
  93.  
  94.         if (!direction) {
  95.             return null;
  96.         }
  97.  
  98.         const nextQueue = this.queue[direction];
  99.         const anotherQueue = this.queue[anotherDirection];
  100.  
  101.         if (nextQueue.length) {
  102.             return nextQueue[0];
  103.         }
  104.  
  105.         return anotherQueue[0];
  106.     },
  107.  
  108.     moveNext() {
  109.         const direction = getDirection();
  110.         const move = {
  111.             [DIRECTION_UP]: this.moveUp,
  112.             [DIRECTION_DOWN]: this.moveDown
  113.         };
  114.  
  115.         if (direction) {
  116.             move[direction]();
  117.         }
  118.     },
  119.  
  120.     onDoorsClosed: function() {
  121.         this.moveNext();
  122.     },
  123.  
  124.     onBeforeFloor: function() {
  125.         const direction = this.getDirection();
  126.         const queue = this.queue[direction];
  127.  
  128.         const currentFloor = this.getCurrentFloor();
  129.         const nextFloor = this.getNextFloor();
  130.  
  131.         if (!nextFloor) {
  132.             return;
  133.         }
  134.  
  135.         if (currentFloor + direction === nextFloor) {
  136.             this.stopAndOpenDoors();
  137.         }
  138.     },
  139.  
  140.     onFloorButtonPressed: function(floor, direction) {
  141.         tihs.addToQueue(floor, direction);
  142.     },
  143.  
  144.     onCabinButtonPressed: function(floor) {
  145.         const currentFloor = this.getCurrentFloor();
  146.  
  147.         if (floor === currentFloor) {
  148.             return;
  149.         }
  150.  
  151.         const direction = floor > currentFloor ? DIRECTION_UP : DIRECTION_DOWN;
  152.  
  153.         this.addToQueue(floor, direction);
  154.     }
  155. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top