# Create a Priority Queue Class return item with higher priority issue

**URL:** <https://forum.freecodecamp.org/t/create-a-priority-queue-class-return-item-with-higher-priority-issue/323992>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [October 25, 2019, 2:43pm UTC](https://forum.freecodecamp.org/t/create-a-priority-queue-class-return-item-with-higher-priority-issue/323992 "2019-10-25T14:43:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lendoo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lendoo/32/177827_2.png) [@lendoo](https://forum.freecodecamp.org/u/lendoo)\
**Post date:** [October 25, 2019, 2:43pm UTC](https://forum.freecodecamp.org/t/create-a-priority-queue-class-return-item-with-higher-priority-issue/323992/1 "2019-10-25T14:43:37Z")

</div>

Hi,  
Maybe have problem of interpretation about this [challenge](https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-priority-queue-class).  
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.

```auto
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());”?

---

<div class="post-metadata">

**Author:** ![lendoo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lendoo/32/177827_2.png) [@lendoo](https://forum.freecodecamp.org/u/lendoo)\
**Post date:** [October 25, 2019, 3:06pm UTC](https://forum.freecodecamp.org/t/create-a-priority-queue-class-return-item-with-higher-priority-issue/323992/3 "2019-10-25T15:06:42Z")

</div>

Ok, my full code is here:

```auto
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());

```

---

<div class="post-metadata">

**Author:** ![lendoo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lendoo/32/177827_2.png) [@lendoo](https://forum.freecodecamp.org/u/lendoo)\
**Post date:** [October 25, 2019, 4:04pm UTC](https://forum.freecodecamp.org/t/create-a-priority-queue-class-return-item-with-higher-priority-issue/323992/5 "2019-10-25T16:04:44Z")

</div>

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

The front method give back: animal… fails on test

My code:

```auto
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());

```

---

<div class="post-metadata">

**Author:** ![camperextraordinaire](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@camperextraordinaire](https://forum.freecodecamp.org/u/camperextraordinaire)\
**Post date:** [October 25, 2019, 4:16pm UTC](https://forum.freecodecamp.org/t/create-a-priority-queue-class-return-item-with-higher-priority-issue/323992/6 "2019-10-25T16:16:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lendoo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lendoo/32/177827_2.png) [@lendoo](https://forum.freecodecamp.org/u/lendoo)\
**Post date:** [October 25, 2019, 4:48pm UTC](https://forum.freecodecamp.org/t/create-a-priority-queue-class-return-item-with-higher-priority-issue/323992/7 "2019-10-25T16:48:08Z")

</div>

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](http://forum.freecodecamp.org/t/freecodecamp-challenge-guide-create-a-priority-queue-class/301630) is very difficult. I tried to understood the logic, but at least I gave up…

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 11:57am UTC](https://forum.freecodecamp.org/t/create-a-priority-queue-class-return-item-with-higher-priority-issue/323992/8 "2021-01-25T11:57:19Z")

</div>

Closed on January 25, 2021, 11:57am UTC


