Tell us what’s happening:
I keep getting the error messages in the FCC console:
// running tests
tempval is undefined
tempval is undefined
tempval is undefined
// tests completed
And I keep failing the last 3 tests:
Your PriorityQueue should correctly keep track of the current number of items using the size
method as items are enqueued and dequeued.
The isEmpty
method should return true
when the queue is empty.
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.
I am lost as to why my code does not pass the tests!? My enqueue function needs a rethink and tidying up but it works in VS Code and I first want to establish why even simple tests like the isEmpty method are not working!
Your code so far
function PriorityQueue() {
this.collection = [];
this.printCollection = function () {
console.log(this.collection);
}
// Only change code below this line
this.enqueue = function (item, priority) {
let inserted = false;
let newarray = [item, priority];
for (let x = this.collection.length - 1; x >= 0; x--) {
if (this.collection[x][1] <= priority) {
this.collection[x + 1] = newarray;
inserted = true;
return;
}
this.collection[x + 1] = this.collection[x];
}
if (inserted == false) {
let tempval = this.collection.shift();
this.collection.shift();
if (newarray[1] < tempval[1]) {
this.collection.unshift(tempval);
this.collection.unshift(newarray);
} else {
this.collection.unshift(newarray);
this.collection.unshift(tempval);
}
}
}
this.dequeue = function () {
return this.collection.shift()[0];
}
this.size = function () {
return this.collection.length;
}
this.front = function () {
return this.collection[0];
}
this.isEmpty = function () {
return (this.collection.length == 0);
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0
.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-priority-queue-class/
- List item