DEV Community

Discussion on: Daily Coding Puzzles - Nov 11th - Nov 16th

Collapse
thesoreon profile image
Paul Susicky
const Queue = {
    in: [],
    out: [],
    enqueue: function(x) {
        this.in.push(x);
    },
    dequeue: function() {
        if (this.out.length == 0) {
            while (this.in.length != 0) {
                this.out.push(this.in.pop());
            }
        }
        return this.out.length == 0 ? "Cannot dequeue" : this.out.pop();
    },
    front: function() {
        if (this.out.length == 0) {
            while (this.in.length != 0) {
                this.out.push(this.in.pop());
            }
        }
        return this.out.length == 0 ? "Cannot show front value" : this.out[this.out.length - 1];
    }
};