JS Built-ins
← Array211 / 226
ArrayArray.prototype.unshiftintermediate

Implement a Queue with unshift

πŸ“– Usage Example
const queue = ['second', 'third']
queue.unshift('first')
queue.shift()  // β†’ 'first'  (FIFO)

Use unshift() with shift() to manage a FIFO queue by adding to the front and removing from it.

Array.prototype.unshift β€” Queue

Using unshift to prepend and pop to dequeue from the end gives you a simple FIFO queue.

Implement enqueue which prepends val using unshift and returns the modified queue.

πŸ’‘ Hints (2)
  • β€’ Call queue.unshift(val) to prepend, then return queue
  • β€’ unshift mutates in place, so you return the same array reference