EXERCISE · AlgoCasts

Weave

js 2) Implement the 'weave' function. Weave receives two queues as arguments and combines the contents of each into a new, third queue.

4 testsAlgoCastsGPL-3.0exercise 15 of 26

Directions

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 different lengths without inserting 'undefined' into the new one. *Do not* access the array inside of any queue, only use the 'add', 'remove', and 'peek' functions.

Examples#

JS
 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'

Try it first#

Copy the stub, make the 4 tests pass on your machine, then open the solution. The reveal is remembered per page in this browser.

JSexercises/weave/index.js
const Queue = require('./queue'); function weave(sourceOne, sourceTwo) {} module.exports = weave;

See also