Hey guys,
I am doing this Priority Queue challenge
challenge
and below is my code so far:
Can someone tell me why it doesn’t work?
function PriorityQueue () {
this.collection = [];
this.printCollection = function() {
console.log(this.collection);
};
// Only change code below this line
this.enqueue = (elem) => {
if(this.isEmpty()) {
this.collection.push(elem)
}
let found_idx = -1;
for(var i = 0; i < this.collection.length; i++){
if(this.collection[i][1] >= elem[1]){
this.collection.push(elem)
found_idx = i
}
}
if (found_index === -1) {
this.collection.push(elem);
} else {
this.swap(found_idx, this.collection.length - 1)
}
}
this.dequeue = () => {
if (!this.isEmpty()) {
return this.collection.shift()[0];
} else {
return "The queue is empty.";
}
}
// item's name
this.front = () => {
return this.collection[0][0]
}
this.size = () => {
return this.collection.length
}
this.isEmpty = () => {
return this.collection.length < 0
}
this.swap = (st, nd) => {
[this.collection[st], this.collection[nd]] = [this.collection[nd], this.collection[st]]
}
// Only change code above this line
}