Create a Priority Queue Class return item with higher priority issue

Hi,
Maybe have problem of interpretation about this challenge.
My code pass the test except the last one.
This is my test array:

  1. (6) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]

0: (2) [“animal”, 2]
1: (2) [“ufo3”, 0]
2: (2) [“ufo2”, 0]
3: (2) [“rabbit”, 2]
4: (2) [“ufo”, 0]
5: (2) [“human”, 1]

The highest priority is: 2. I have two items with the highest priority:
0: (2) [“animal”, 2] and 3: (2) [“rabbit”, 2]
The last is 3: (2) [“rabbit”, 2] and the priorityQueue.front() returns “rabbit”.
This return value is fails on the last test… What have to send return if not “rabbit”?
If need I will provide the full code, but maybe I have only the problem of interpretation.

class PriorityQueue {
... my full code is here...
}

const priorityQueue = new PriorityQueue;
priorityQueue.printCollection(); // []
console.log(priorityQueue.dequeue()); // undefined
console.log(priorityQueue.isEmpty()); // true
priorityQueue.enqueue(["human", 1]);
priorityQueue.enqueue(["ufo", 0]);
priorityQueue.enqueue(["rabbit", 2]);
priorityQueue.enqueue(["ufo2", 0]);
priorityQueue.enqueue(["ufo3", 0]);
priorityQueue.enqueue(["animal", 2]);
priorityQueue.printCollection(); // my above test array
console.log(priorityQueue.front()); // rabbit

after the row of above commands what have to give back the “console.log(priorityQueue.front());”?

Ok, my full code is here:

class PriorityQueue {
    constructor() {
        this.collection = [];
    };
    printCollection() {
        console.log(this.collection)
    };
    enqueue(value) {
        if (typeof value === "object" && 
            value.length === 2 && 
            typeof value[1] === "number") {
            return this.collection.unshift(value); // returns the new length 
        }
    };
    dequeue() {
        if (this.collection.length > 0) return this.collection.pop()[0];
    };
    front() {
        let highest = -Infinity;
        const priority = this.collection.forEach(value => {
            if (value[1] > highest) highest = value[1];    
        });
        const priorityArray = this.collection.filter(value => value[1] === highest);
        return priorityArray[priorityArray.length - 1][0];
    };
    size() {
        return this.collection.length;
    };
    isEmpty() {
        return this.collection.length === 0;
    };
};
const priorityQueue = new PriorityQueue;
priorityQueue.printCollection();
console.log(priorityQueue.dequeue());
console.log(priorityQueue.isEmpty());
priorityQueue.enqueue(["human", 1]);
priorityQueue.enqueue(["ufo", 0]);
priorityQueue.enqueue(["rabbit", 2]);
priorityQueue.enqueue(["ufo2", 0]);
priorityQueue.enqueue(["ufo3", 0]);
priorityQueue.enqueue(["animal", 2]);
priorityQueue.printCollection();
console.log(priorityQueue.front());

I changed the enqueue() method. Now the printCollection() give back the right sequence.

The front method give back: animal… fails on test

My code:

class PriorityQueue {
    constructor() {
        this.collection = [];
    };
    printCollection() {
        console.log(this.collection)
    };
    enqueue(value) {
        if (typeof value === "object" && value.length === 2 && typeof value[1] === "number") {
            let index = 0;
            while (this.collection[index] && this.collection[index][1] < value[1] + 1) {
                index ++;
            }
            this.collection.splice(index, 0, value);
        }
    };
    dequeue() {
        if (this.collection.length > 0) return this.collection.pop()[0];
    };
    front() {
       return this.collection[this.collection.length - 1][0];
    };
    size() {
        return this.collection.length;
    };
    isEmpty() {
        return this.collection.length === 0;
    };
};
const priorityQueue = new PriorityQueue;
priorityQueue.printCollection();
console.log(priorityQueue.dequeue());
console.log(priorityQueue.isEmpty());
priorityQueue.enqueue(["human", 1]);
priorityQueue.enqueue(["ufo", 0]);
priorityQueue.enqueue(["rabbit", 2]);
priorityQueue.enqueue(["ufo2", 0]);
priorityQueue.enqueue(["ufo3", 0]);
priorityQueue.enqueue(["animal", 2]);
priorityQueue.printCollection();
console.log(priorityQueue.front());
1 Like

Now that you have fixed your enqueue method, you need to fix your dequeue method.

1 Like

Thank you very much indeed for your help… At least I found the issue. From the array have to delete the first element, not the last. I changed the pop() method, and now I pass the test

Thanks.

P.s: Once I pass a test I always go to see other solutions on the forum to check different solutions and take some idea how to resolve problems on other way…
The solution of guide is very difficult. I tried to understood the logic, but at least I gave up…