Is there any way I can reduce this code to do the same thing but with 100 less characters?
It\'s a simple double edge queue that has pushHead,popHead,pushTail,popTail, a
You could get rid of the manual array handling. I guess you can't get any shorter than this (you could shorten variable names, but the code probably needs to be at least this long).
function Deque() {}
Deque.prototype = new Array();
var prot = Deque.prototype;
prot.pushHead = Deque.prototype.unshift;
prot.popHead = Deque.prototype.shift;
prot.pushTail = Deque.prototype.push
prot.popTail = Deque.prototype.pop
prot.isEmpty = function() {return this.length == 0}
This way, you also get all the functionality of the default Arrays
as well. Deque
in this example effectively a sub-class of the Array
class.