Data Structures - Create a Priority Queue Class

Tell us what’s happening:
Could someone explain to me why my code is failing tests for displaying element at the front of the queue and also logging all elements. I have tested with multiple arrays and different insertion orders and the output seems to be OK. Any help will be highly appreciated!

Your code so far

function PriorityQueue () {
  this.collection = [];
  this.printCollection = function() {
    console.log(this.collection);
  };
  // Only change code below this line
  this.enqueue = function(element){
    let indexToInsert = 0;
    for (let i=0;i<this.collection.length;i++){
      const val = this.collection[i];
      if(element[1] >= val[1]){
        indexToInsert = i;
      }
    }
    if(indexToInsert){
        indexToInsert++;
    }
    this.collection.splice(indexToInsert, 0, element)
  }

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

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

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

  this.isEmpty = function() {
    return this.collection.length < 1;
  }
  // 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/111.0.0.0 Safari/537.36

Challenge: Data Structures - Create a Priority Queue Class

Link to the challenge:

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