Create a Priority Queue Class fails on all test

Create a Priority Queue Class

My code bellow fails on all test but as I see works fine all method… Maybe I cannot to override the constructor?

Summary
class Queue {
    constructor() {
        this.collectionIn = {};
        this.collectionOut = {};
        this.priority = new Set();
        this.min = Infinity;
        this.sized = 0;
    }
    
    printCollection() { 
        console.log(this.collection); 
    }
    
    enqueue(value) {
        if (typeof value === "object" && value.length === 2 && typeof value[1] === "number") {
            if (!(this.priority.has(value[1]))) this.priority.add(value[1]);
            if (value[1] < this.min) this.min = value[1]; // setup the minimum if need
            if (this.collectionIn.hasOwnProperty(value[1])) {
                // this priority-collection already exist
                this.collectionIn[value[1]].push(value[0]);
            } else {
                // still not exists, let's make one
                this.collectionIn[value[1]] = [value[0]];
                this.collectionOut[value[1]] = [];
            }
            this.sized ++;
        }
    }
    
    dequeue() {
        const minimum = this.min;
        if (minimum === Infinity) return null;
        const collIn = this.collectionIn[minimum];
        const collOut = this.collectionOut[minimum];
        if (!(collOut.length)) {
            // out-collection  empty, have to fill up from the in-collection
            while (collIn.length) {
                collOut.push(collIn.pop());
            }
        }
        const result = collOut.pop();
        if (collIn.length + collOut.length === 0) {
            // Must delete this priority from the Set:
            this.priority.delete(minimum);
            this.min = Math.min(...this.priority); // setup the new minimum
        }
        this.sized --;
        return result;
    }
    
    size() { 
        return this.sized; 
    }
    
    front() {
        if (this.min === Infinity) this.min = Math.min(...this.priority); // if the Set empty return Infinity
        if (this.min === Infinity) return null;
        const minimum = this.min;
        const priorityCollection = this.collectionOut[minimum];
        if (priorityCollection.length) return priorityCollection[priorityCollection.length - 1];
        return this.collectionIn[minimum][0];
    }
    
    isEmpty() { 
        return this.sized ? false : true; 
    } 
}

const myPriorityQueueObj = new PriorityQueueObj;
console.log(myPriorityQueueObj);
myPriorityQueueObj.enqueue(["human", 1]);
myPriorityQueueObj.enqueue(["kitten", 2]);
myPriorityQueueObj.enqueue(["dog", 2]);
myPriorityQueueObj.enqueue(["rabbit", 2]);
//console.log(myPriorityQueueObj.dequeue());
//console.log(myPriorityQueueObj.dequeue());
//console.log(myPriorityQueueObj.dequeue());
//console.log(myPriorityQueueObj.dequeue());
//console.log(myPriorityQueueObj.dequeue());
console.log(myPriorityQueueObj.dequeue());
//myPriorityQueueObj.enqueue(["human", 1]);
console.log(myPriorityQueueObj.dequeue());
console.log(myPriorityQueueObj.dequeue());
myPriorityQueueObj.enqueue(["monkey", -1]);
console.log(myPriorityQueueObj.dequeue());
console.log(myPriorityQueueObj.size());
console.log(myPriorityQueueObj.front());
console.log(myPriorityQueueObj.isEmpty());
console.log(myPriorityQueueObj.min);

@lendoo The original function name in the editor was PriorityQueue. The test text needs to be changed. Change the class name from Queue to PriorityQueue and you are good to go.

Opened an issue for this #37842. I’d like to fix it once I get home, if no one beats me to it :smiley:

You can close the issue, because I created a PR for it yesterday.

How can I close it?
Tick on solution? Or need to delete this post?

@lendoo My previous reply was actually meant for @partylich. You do not need to do anything.