Last test-case giving error

Hi all, the last test case of ‘Create a Priority Queue Class’ is not passing; I checked the order in my terminal, it is perfectly working. may be I missing something from question itself; Help needed!


    function PriorityQueue () {
      this.collection = [];
      this.printCollection = function() {
       console.log(this.collection);
     };
     var qSize = 0;
    this.priorities = [];

    this.size = function(){
      return qSize;
    }
    this.isEmpty = function(){
      return (qSize == 0);
    }
    this.enqueue = function(elem, priority){
      if(this.size() == 0){
        this.collection[qSize] = elem;
        this.priorities[qSize] = priority;
        qSize++;

        return;
      }

      for(var i=0; i<qSize; i++){
        if(this.priorities[i] > priority){
          this.collection.splice(i, 0, elem);
          this.priorities.splice(i, 0, priority);
          qSize++;

          return;
        }
      }

      this.collection[this.collection.length] = elem;
      this.priorities[this.priorities.length] = priority;
      qSize++;

      return;
    }
    this.dequeue = function(){
      if(this.isEmpty())
          return;

      var temp = this.collection[qSize];
      var temp1 = this.priorities[qSize];

      qSize--;

      return temp;
    }
}

:frowning:

You can validate your PriorityQueue does not work correctly, if you test it like:

var test = new PriorityQueue();
test.enqueue(['A', 5]);
test.enqueue(['B', 5]);
test.enqueue(['C', 5]);
test.enqueue(['D', 3]);
test.enqueue(['E', 1]);
test.enqueue(['F', 7]);
console.log(test.printCollection());

The console.log should yield:

[
  ['E', 1],
  ['D', 3],
  ['A', 5],
  ['B', 5],
  ['C', 5],
  ['F', 7]
]

Instead, yours yields:

[
  ['A', 5],
  ['B', 5],
  ['C', 5],
  ['D', 3],
  ['E', 1],
  ['F', 7]
]

Basically, your solution just keeps the items in the original order as they were added to the queue.