Create a Priority Queue Class(last test case is not passing)

Create a Priority Queue Class(last test case is not passing)
0

#1

Tell us what’s happening:
This is working right in console.log but it is not passing last test case when i am submitting this code.
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.

Your code so far


function PriorityQueue() {
  this.collection = [];
  this.printCollection = function () {
    console.log(this.collection);
  };
  // Only change code below this line
  this.enqueue = function (val) {
    console.log('enqueue', val)
    this.collection.push(val)
  }
  this.dequeue = function () {
    let highestPriority = {
      index: 0,
      priority:-1, 
    }
    this.collection.forEach( (element, index) => {
      if(highestPriority.priority < element[1]) {
        highestPriority.priority = element[1]
        highestPriority.index = index
      }
    })
    
    let dequeueElement = this.collection[highestPriority.index]
    this.collection.splice(highestPriority.index, 1)
    console.log(
      'dequeue',highestPriority.index,
      dequeueElement[0],this.collection
    )
    return dequeueElement[0] 
  }
  this.size = function () {
    return this.collection.length
  }
  this.front = function () {
    const firstElement = this.collection[0]
    return firstElement
  }
  this.isEmpty = function () {
    return this.collection.length === 0 ? true : false
  }
  // Only change code above this line
} 

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-priority-queue-class


#2

i haven’t checked your code but I wanted to tell you that I had a similar experience with a different challenge (where my console.log showed the exact answer expected but my code wasn’t getting accepted).
I’m not sure exactly what happened, but when I cut the code out and re-pasted a fresh copy and re-ran, it worked.
I still have no idea why that happened, but maybe you can try it.


#3

Have you come up with the answer yet?

It looks to me there is a problem with the first method enqueue. It is just placing the element in the last position and not in the corresponding priority order.