Tell us what’s happening:
This code isn’t passing the last 3 test cases. Please help.
Your code so far
function PriorityQueue () {
this.collection = [];
let rear = -1;
this.printCollection = function() {
console.log(this.collection);
};
// Only change code below this line
this.enqueue = (x) => {
if (!Array.isArray(x) || x.length !== 2) return console.log("Invalid input");
else if (typeof this.collection !== 'undefined' && this.collection.length > 0) rear = this.collection.length - 1;
else if (rear > this.collection.length - 1) return console.log("Queue Overflow");
let i = 0;
this.collection.map((item) => {
if (item[1] > x[1]) return;
i++;
});
if (i == 0) this.collection.unshift(x);
else if (i == this.collection.length) this.collection.push(x);
else {
let arr = this.collection.splice(i);
this.collection.push(x);
this.collection = this.collection.concat(arr);
}
}
this.dequeue = () => {
if (rear <= 0) return console.log("Queue Underflow");
rear--;
return this.collection.shift()[0];
}
this.size = () => {
return this.collection.length;
}
this.front = () => {
return this.collection[0][0];
}
this.isEmpty = () => {
return this.collection.length <= 0;
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) 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