Tell us what’s happening:
passing all test except the last one. no idea why. any help would be greatly appreciated .
thank you in advance
Your code so far
function PriorityQueue () {
this.collection = [];
this.printCollection = function() {
console.log(this.collection);
};
// Only change code below this line
this.enqueue = (item) => {
if (this.isEmpty()) {
this.collection = [item];
return;
}
const queue = this.collection;
const priority = item[1];
let i = this.collection.findIndex(function (item) {
return priority < item[1];
});
if (i === -1) {
this.collection = [...queue, item]
} else {
this.collection = [
...queue.slice(0, i), item, ...queue.slice(i)
]
}
}
this.dequeue = () => {
if (this.isEmpty()) return;
this.collection = this.collection.slice(1)
return this.front()
}
this.size = () => {
return this.collection.length
}
this.front = () => {
if (!this.isEmpty()) return this.collection[0][0];
}
this.isEmpty = () => {
return this.size() <= 0;
}
// Only change code above this line
}
const myQ = new PriorityQueue()
myQ.collection = [['human', 1], ['kitten', 2], ['dog', 2], ['rabbit', 2], ['tiger', 3]]
console.log('front, ', myQ.front())
console.log('enqueue, ', `['wolf', 0]`)
myQ.enqueue(['wolf', 2.5])
console.log('front, ', myQ.front())
console.log('dequeued, ', myQ.dequeue())
console.log('front, ', myQ.front())
myQ.printCollection()
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36
.
Challenge: Create a Priority Queue Class
Link to the challenge: