1
+ // // # --- Directions
2
+ // * Complete the task in weave/queue.js
3
+ // * Implement the 'weave' function. Weave
4
+ // receives two queues as arguments and combines the
5
+ // contents of each into a new, third queue.
6
+ // The third queue should contain the *alterating* content
7
+ // of the two queues. The function should handle
8
+ // queues of different lengths without inserting
9
+ // 'undefined' into the new one.
10
+ // *Do not* access the array inside of any queue, only
11
+ // use the 'add', 'remove', and 'peek' functions.
12
+
13
+
14
+ // # --- Example
15
+ // const queueOne = new Queue();
16
+ // queueOne.add(1);
17
+ // queueOne.add(2);
18
+ // const queueTwo = new Queue();
19
+ // queueTwo.add('Hi');
20
+ // queueTwo.add('There');
21
+ // const q = weave(queueOne, queueTwo);
22
+ // q.remove() should return 1
23
+ // q.remove() should return 'Hi'
24
+ // q.remove() should return 2
25
+ // q.remove() should return 'There'
26
+
27
+ // Solution
28
+
29
+ const Queue = require ( './queue' ) ;
30
+
31
+ function weave ( sourceOne , sourceTwo ) {
32
+ const q = new Queue ( ) ;
33
+
34
+ while ( sourceOne . peek ( ) || sourceTwo . peek ( ) ) {
35
+ if ( sourceOne . peek ( ) ) {
36
+ q . add ( sourceOne . remove ( ) ) ;
37
+ }
38
+
39
+ if ( sourceTwo . peek ( ) ) {
40
+ q . add ( sourceTwo . remove ( ) ) ;
41
+ }
42
+ }
43
+
44
+ return q ;
45
+ }
0 commit comments