Data Structures: Create a Priority Queue Class fail

Data Structures: Create a Priority Queue Class fail
0

#1

My code for priority queue is failing one test: “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.”

Not sure of the reason why, but I’ve checked the queue and it’s the order I’m expecting. Tried resetting the challenge and resubmitting the code but still the same.

Here is my code:

function PriorityQueue () {
    this.collection = [];
    this.printCollection = function() {
      console.log(this.collection);
    };
    // Only change code below this line

    var n = this.collection.length;
    this.enqueue = function(newItem) {
        if (n == 0 && this.collection.indexOf(newItem) == -1) {
            this.collection.splice(0, 0, newItem);
            n = this.collection.length;
        } else {
            this.collection.forEach((x, index) => {
                if (x[1] > newItem[1] && this.collection.indexOf(newItem) == -1) {                                        // If item in collection has bigger priority value than newItem priority value
                    this.collection.splice(index, 0, newItem);
                    n = this.collection.length;
            } else {                                                                                                    // If item in collection has lesser priority value than newItem priority value
                    if (index + 1 == n && this.collection.indexOf(newItem) == -1) {
                        this.collection.splice(index + 1, 0, newItem);
                        n = this.collection.length;
                    } 
                }
            });
        }

        this.collection.forEach((y) => {
            console.log(y);
        });
    }

    this.dequeue = function() {
        var removedElem = this.collection.shift();
        n = this.collection.length;
        return removedElem;
    }

    this.size = function() {
        return n;    
    }

    this.front = function() {
        return this.collection[0];
    }

    this.isEmpty = function() {
        if (n == 0) {
            return true;
        }
        return false;
    }
	// Only change code above this line
}

#2

Your code does what it should to me.

There might be a bug with this challenge.


#3

@shimphillip Thanks for checking! I thought so too.

As a test, I’ve also tried the working solution from Create a Priority Queue Class and we got the same output on all the test cases that the challenge provided.

I’ll have to move on to the next challenge then.


#4

The problem is your dequeue method.

this.dequeue = function() {
  var removedElem = this.collection.shift();
  n = this.collection.length;
  return removedElem;
}

The instructions state the following:

The dequeue should return only the current item, not its priority.

Instead your dequeue method returns an array containing both the item and it’s priority. Just return the first element of this array.


#5

@RandellDawson Thank you! Yeah, I see that now. It does return the array, I thought I fixed it previously but i removed the logs when I was cleaning the code and didn’t catch this expected value from dequeue.