Tell us what’s happening:
This is working right in console.log but it is not passing last test case when i am submitting this code.
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.
Your code so far
function PriorityQueue() {
this.collection = [];
this.printCollection = function () {
console.log(this.collection);
};
// Only change code below this line
this.enqueue = function (val) {
console.log('enqueue', val)
this.collection.push(val)
}
this.dequeue = function () {
let highestPriority = {
index: 0,
priority:-1,
}
this.collection.forEach( (element, index) => {
if(highestPriority.priority < element[1]) {
highestPriority.priority = element[1]
highestPriority.index = index
}
})
let dequeueElement = this.collection[highestPriority.index]
this.collection.splice(highestPriority.index, 1)
console.log(
'dequeue',highestPriority.index,
dequeueElement[0],this.collection
)
return dequeueElement[0]
}
this.size = function () {
return this.collection.length
}
this.front = function () {
const firstElement = this.collection[0]
return firstElement
}
this.isEmpty = function () {
return this.collection.length === 0 ? true : false
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-priority-queue-class