I am trying to use .sort() to greatly simplify this, compared to other solutions I’ve seen. It appears that my collection has the items in the right order by priority and input order, but I’m not passing the isEmpty or first-in-first-out tests ( the last two tests )
function PriorityQueue () {
this.collection = [];
this.printCollection = function() {
console.log(this.collection);
};
// Only change code below this line
this.enqueue = function(arr){
this.collection.push(arr)
this.collection.sort((a,b)=>{
return a[1] - b[1]
})
console.log('After enqueue',this.collection)
}
this.dequeue = function(){
this.collection.shift()
console.log('After dequeue',this.collection)
return this.collection[0][0]
}
this.front = function(){
console.log('Front',this.collection[0])
return this.collection[0]
}
this.size = function(){
console.log('Size',this.collection.length)
return this.collection.length
}
this.isEmpty = function(){
console.log('Empty',this.collection,this.size() === 0)
return this.size() === 0
}
// Only change code above this line
}
const demo = new PriorityQueue()
Any ideas?