Use case of this queue

I have this code after following one tutorial, but he didn’t test it unfortunately in the end:

class Queue {
  constructor() {
    this.data = [];
  }
  add(record) {
    this.data.unshift(record);
  }
  remove() {
    return this.data.pop();
  }
  peek() {
    return this.data[this.data.length - 1];
  }
}

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;
}

weave([1, 3, 5], [2, 4, 6]);

So, when I run this function with two arrays: I get the following error:

Uncaught TypeError: sourceOne.peek is not a function
    at weave 

How should I use this weave function if not with two arrays?

Thanks Randell, I understand now. Yes, the intent was to alternate values of queues. I thought it could be used with arrays.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.