Here is my code
function PriorityQueue () {
this.collection = [];
this.printCollection = function() {
console.log(this.collection);
};
// Only change code below this line
this.enqueue = function(item) {
let i = this.collection.findIndex(iCol => iCol[1] >= item[1])
if(i < 0) {
this.collection.push(item)
} else {
this.collection.splice(i, 0, item)
}
}
this.dequeue = function() {
if(this.isEmpty()) return
return this.collection.shift()[0]
}
this.front = function() {
if(this.isEmpty()) return
return this.collection[0][0]
}
this.size = function() {
return this.collection.length
}
this.isEmpty = function() {
return this.collection.length == 0
}
// Only change code above this line
}
const priorityQueue = new PriorityQueue;
priorityQueue.enqueue(["human", 1]);
priorityQueue.enqueue(["ufo", 0]);
priorityQueue.enqueue(["rabbit", 2]);
priorityQueue.enqueue(["ufo2", 0]);
priorityQueue.enqueue(["ufo3", 0]);
priorityQueue.enqueue(["animal", 2]);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36
.
Challenge: Create a Priority Queue Class
Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-priority-queue-class