Data Structures: Create a Priority Queue Class-Not Working

My code for the Priority Queue Class is failing. I get the error “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 haven’t yet figured out why it’s not queueing in order. Everything else is green lighted. Any assistance/info would be greatly appreciated.

Here is my code below.

``````function PriorityQueue () {
this.collection = [];
this.printCollection = function() {
console.log(this.collection);
};
// Only change code below this line
this.size = () => {return this.collection.length;};
this.isEmpty = () => {return this.collection.length === 0;};
this.dequeue = () => {return this.collection.shift()[0];};
this.front = () => {return this.collection[0][0];};
this.enqueue = data => {
if(this.isEmpty()){
this.collection.push(data);
}
else {
let insertionpoint = 0;
while(insertionpoint < this.size() && this.collection[insertionpoint][1] < data[1]){
insertionpoint += 1;
}
this.collection.splice(insertionpoint,0,data);
}
};
// Only change code above this line
}
``````

It looks like you are supposed to use an array that goes from highest priority to lowest to make the priority queue, but your enqueue method seems to build an array that goes lowest priority to highest.

Let’s start with an queue with an element of priority 5, and say you want insert an element of priority 6 to the queue. Base on your code, you check index 0, see that the priority is 5 which is less than 6, so you increment the insertion point to 1. Then you exit the while loop and splice in element with priority 6 at index 1.

So you end up with [5,6], but your priority queue should be [6,5]