Create a Priority Queue Class - Cannot Pass!

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/

  1. List item

When you use this, it will reference the closest function.

this.isEmpty = function () {
   return (this.collection.length == 0);
}

What that means is that this.collection here is not referencing the collection you want (from the parent function), it is basically undefined.

You can use arrow functions, because them do not bind this to itself.

1 Like

Hi @ghukahr,

I rewrote my code using the arrow functions and it looked a lot neater and it was a good lesson for me in how to implement them and the effect they have on ‘this’. It turns out that it was the messy ‘enqueue’ function that I had written that was causing the problems and it was also causing the other test to fail too for some reason!?

Many thanks for the feedback and assistance with this one. Here’s the final code - much better!

function PriorityQueue() {
    this.collection = [];
    this.printCollection = function () {
        console.log(this.collection);
    };
    // Only change code below this line
    this.enqueue = (newarray) => {
        let pushed = false;
        let priority = newarray[1];
        let newcollection = [];
        if (this.collection.length == 0) {
            this.collection.push(newarray);
            return;
        }
        for (let x = 0; x < this.collection.length; x++) {
            if (this.collection[x][1] > priority && pushed == false) {
                newcollection.push(newarray);
                pushed = true;
            }
            newcollection.push(this.collection[x]);
        }
        if (pushed == false) newcollection.push(newarray);
        this.collection = newcollection;
        return;
    }

    this.dequeue = () => this.collection.shift()[0];
    this.size = () => this.collection.length;
    this.front = () => this.collection[0];
    this.isEmpty = () => this.collection.length == 0;
    // Only change code above this line
}