-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (44 loc) · 1.29 KB
/
index.js
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
// --- Directions
// 1) Complete the task in weave/queue.js
// 2) Implement the 'weave' function
// Weave receives two queues as arguments and combines
// the contents of each into a new, third queue.
// The third queue should contain the *alterating* content
// of the two queues. The function should handle
// queues of diff lengths without inserting 'undefined
// into the new one
// Do not access the array inside of any queue, only use
// 'add', 'remove', and 'peek' functions
/*
Source Queue One
hi to you ------>
-> 1 hi 2 to 3 you
1 2 3 ------>
Source Queue Two
*/
// --- Examples
// const queueOne = new Queue();
// queueOne.add(1);
// queueOne.add(2);
// const queueTwo = new Queue();
// queueTwo.add('Hi');
// queueTwo.add('There');
// const q = weave(queueOne, queueTwo);
// q.remove(); // 1
// q.remove(); // 'Hi'
// q.remove(); // 2
// q.remove(); // 'There'
const Queue = require("./queue");
function weave(sourceOne, sourceTwo) {
const q = new Queue();
while (sourceOne.peek() || sourceTwo.peek()) {
if (sourceOne.peek()) {
q.add(sourceOne.remove());
}
if (sourceTwo.peek()) {
q.add(sourceTwo.remove())
}
}
return q;
}
module.exports = weave;