Getting Correct Output but Not Passing the Test -- "Create a Priority Queue Class"

It appears to me that I am getting the output that matches the specifications in the challenge, but when I run the tests, the system is telling me that it doesn’t pass the “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.” test.

Here is my code (with some commands at the end):

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

    this.enqueue = function(item, priority) {
      let i = 0;

      if (this.collection.length == 0) {
      this.collection.splice(i, 0, [item, priority]);
      } else {
      while (this.collection[i][1] <= priority ) { 
        i++; 
        if (i == (this.collection.length)) {
          break;
          }
        }
      this.collection.splice(i, 0, [item, priority]);
      }
      this.collection.printCollection;
    }

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

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

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

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

    // Only change code above this line
}
let myQueue = new PriorityQueue;
myQueue.enqueue ('kitten', 2);
myQueue.enqueue ('dog', 2);
myQueue.enqueue ('rabbit', 2);
myQueue.enqueue ('human', 1);
myQueue.enqueue ('angel', 1);

myQueue.printCollection();

console.log(myQueue.dequeue ());

myQueue.printCollection();

console.log(myQueue.front());

And this is the output:

[ [ 'human', 1 ],
  [ 'angel', 1 ],
  [ 'kitten', 2 ],
  [ 'dog', 2 ],
  [ 'rabbit', 2 ] ]
human
[ [ 'angel', 1 ], [ 'kitten', 2 ], [ 'dog', 2 ], [ 'rabbit', 2 ] ]
angel

That’s what I should get, right?

@JoanHall The problem is, according to the challenge description and instructions, the enqueue method only accepts one argument (an array with two elements). You are passing two arguments to the enqueue method.

Instead of testing like:

myQueue.enqueue ('kitten', 2);
myQueue.enqueue ('dog', 2);
myQueue.enqueue ('rabbit', 2);
myQueue.enqueue ('human', 1);
myQueue.enqueue ('angel', 1);

you should be testing like::

myQueue.enqueue (['kitten', 5]);
myQueue.enqueue (['dog', 5]);
myQueue.enqueue (['rabbit', 5]);
myQueue.enqueue (['human', 3]);
myQueue.enqueue (['angel', 1]);
1 Like

Thanks! I’ll get right on that.