Created
July 11, 2016 04:57
-
-
Save jasonwaters/3dfce7b2530af863da8670ad3ca477e7 to your computer and use it in GitHub Desktop.
Drone Deploy Coding Exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Below is a drone object with six methods. | |
| // Each method takes a callback and after some amount of time will execute that callback (for ease of testing, it will also console.log a value). | |
| // At the bottom of the page the expected output is logged. | |
| // And we invoke the chained call of the drone object. However the drone object does not yet have a chained api. | |
| // Your job is to add a chained api to the drone object by only modifying the code in the middle of the page. | |
| // A good answer should include the following criteria. | |
| // 1. Code was only added between the below comments. | |
| // 2. Each method in the chain executes only after the previous method has resolved | |
| // 3. Your solution should be flexible to rearranging the methods or changing the timeouts. | |
| // 4. Your solution should not change the behavior/side affects of the original methods. You should assume that you don't know how the methods on the Drone object are implemented. Only that they may or may not be asynchronous, and that they take a callback that will be called when the action is completed. | |
| function Drone(){}; | |
| Drone.prototype.takeoff = function takeoff(callback) { | |
| setTimeout(function() { | |
| callback() | |
| console.log('Took off'); | |
| }, 600); | |
| }; | |
| Drone.prototype.turnOnCamera = function turnOnCamera(callback) { | |
| setTimeout(function() { | |
| callback() | |
| console.log('Camera turned on'); | |
| }, 1000); | |
| }; | |
| Drone.prototype.pointDownGimbal = function pointDownGimbal(callback) { | |
| setTimeout(function() { | |
| callback() | |
| console.log('Gimbal pointing down'); | |
| }, 750); | |
| }; | |
| Drone.prototype.flyToMission = function FlyToMission(callback) { | |
| setTimeout(function() { | |
| callback() | |
| console.log('Flown to mission'); | |
| }, 2000); | |
| }; | |
| Drone.prototype.takePhoto = function takePhoto(callback) { | |
| setTimeout(function() { | |
| callback() | |
| console.log('Photo taken'); | |
| }, 500); | |
| }; | |
| Drone.prototype.land = function land(callback) { | |
| setTimeout(function() { | |
| callback() | |
| console.log('Landed'); | |
| }, 3000); | |
| }; | |
| //DON'T MODIFY ANYTHING ABOVE HERE | |
| // START ADD YOUR CODE HERE | |
| function ChainableDrone(){ | |
| Drone.call(this, arguments); | |
| this.queue = []; | |
| this.queueTime = null; | |
| } | |
| ChainableDrone.prototype.next = function() { | |
| if(this.queue.length > 0) { | |
| var methodName = this.queue.shift(); | |
| Drone.prototype[methodName].call(this, function() { | |
| this.next(); | |
| }.bind(this)); | |
| } | |
| } | |
| ChainableDrone.prototype.enqueue = function(name) { | |
| this.queue.push(name); | |
| if(this.queueTime) { | |
| clearTimeout(this.queueTime); | |
| this.queueTime = null; | |
| } | |
| if(this.queue.length > 0) { | |
| this.queueTime = setTimeout(function() { | |
| this.next(); | |
| }.bind(this), 100); | |
| } | |
| } | |
| ChainableDrone.prototype.takeoff = function() { | |
| this.enqueue('takeoff'); | |
| return this; | |
| } | |
| ChainableDrone.prototype.turnOnCamera = function(callback) { | |
| this.enqueue('turnOnCamera'); | |
| return this; | |
| } | |
| ChainableDrone.prototype.pointDownGimbal = function(callback) { | |
| this.enqueue('pointDownGimbal'); | |
| return this; | |
| } | |
| ChainableDrone.prototype.flyToMission = function(callback) { | |
| this.enqueue('flyToMission'); | |
| return this; | |
| } | |
| ChainableDrone.prototype.takePhoto = function(callback) { | |
| this.enqueue('takePhoto'); | |
| return this; | |
| } | |
| ChainableDrone.prototype.land = function(callback) { | |
| this.enqueue('land'); | |
| return this; | |
| } | |
| ChainableDrone.constructor = Drone; | |
| var drone = new ChainableDrone(); | |
| // END ADD YOUR CODE HERE | |
| //DONT MODIFY ANYTHING BELOW HERE | |
| console.log("Expected Output:") | |
| console.log("Took off"); | |
| console.log("Camera turned on"); | |
| console.log("Gimbal pointing down"); | |
| console.log("Flown to mission"); | |
| console.log("Photo taken"); | |
| console.log("Landed"); | |
| console.log("\n\nActual Output:") | |
| drone.takeoff().turnOnCamera().pointDownGimbal().flyToMission().takePhoto().land(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment