Data Structures - Create a Priority Queue Class

Tell us what’s happening:

I don’t see the error in my solution.

The test says it is failing in two places:

  1. The front method should return the correct item at the front of the queue as items are enqueued and dequeued.

  2. The priority queue should return items with a higher priority before items with a lower priority and return items in first-in-first-out order otherwise.

I believe my solution is correct, but it does not pass. I can’t seem to locate the error. Please help.

Your code so far

function PriorityQueue () {
  this.collection = [];
  this.printCollection = function() {
    console.log(this.collection);
  };
  // Only change code below this line

  this.enqueue = function(type, priorityNumber) {
    let newItem = [type, priorityNumber];
    let added = false;
    for (let i = 0; i < this.collection.length; i++) {
      if (priorityNumber < this.collection[i][1]) {
        this.collection.splice(i, 0, newItem);
        added = true;
        break;
      }
    }
    if (!added) {
      this.collection.push(newItem);
    }
    return this.front();
  }

  this.dequeue = function() {
    return this.collection.shift()[0];
  }

  this.size = function() {
    return this.collection.length;
  }

  this.front = function() {
    if (this.isEmpty()) {
      return null;
    }
    return this.collection[0][0];
  }

  this.isEmpty = function() {
    return this.collection.length === 0;
  }
  // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

Data Structures - Create a Priority Queue Class

Take another look at the instructions regarding enqueueing. There’s little detail in what is expected to be input for the enqueue method.