-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
67 lines (46 loc) · 1.37 KB
/
queue.js
File metadata and controls
67 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
const mocha = require('mocha');
const { assert } = require('chai');
function MyQueue(...elements) {
this.queue = elements;
this.setup = function(reverse) {
this.reverse = reverse;
};
this.enqueue = function(...elements) {
if(this.reverse)
return this.queue.unshift(...elements);
return this.queue.push(...elements);
};
this.dequeue = function() {
if(this.reverse)
return this.queue.pop();
return this.queue.shift();
};
this.ping = function() {
console.log(this.queue);
};
};
describe("Cool Queues", () => {
it("Push to the left", () => {
const myQ = new MyQueue('four', 'five');
myQ.setup(true);
assert.equal(myQ.enqueue('one', 'two', 'three'), 5);
assert.deepEqual(myQ.queue, ['one', 'two', 'three', 'four', 'five']);
});
it("Pop item from the right", () => {
const myQ = new MyQueue('one', 'two', 'three');
myQ.setup(true);
assert.equal(myQ.dequeue(), 'three');
});
it("Push to the right", () => {
const myQ = new MyQueue('four', 'five');
myQ.setup(false);
assert.equal(myQ.enqueue('one', 'two', 'three'), 5);
assert.deepEqual(myQ.queue, ['four', 'five', 'one', 'two', 'three']);
});
it("Pop item from the left", () => {
const myQ = new MyQueue('one', 'two', 'three');
myQ.setup(false);
assert.equal(myQ.dequeue(), 'one');
});
});