[Bug?] Data Structures: Create a Priority Queue Class

My code below:
(Updated Feb 7th 16:40)

function PriorityQueue () {
    this.collection = [];
    this.printCollection = function() {
      console.log(this.collection);
    };

    this.enqueue = (item) => {
      if(this.collection.length == 0){
        this.collection.push(item)
      }
      else{
        for(let i=0; i<this.collection.length; i++){
          if(this.collection[i][1] < item[1]){
            this.collection.splice(i,0,item)
            break
          }
          if(i == this.collection.length-1){
            this.collection.push(item)
            break
          }
        }
      }
    }
    this.dequeue = () => {
      return this.collection.shift()
    }
    this.size = () => {
      return this.collection.length
    }
    this.front = () => {
      return this.collection[0]
    }
    this.isEmpty = () => {
      if(this.collection.length === 0){
        return true
      }
      else{
        return false
      }
    }

}

Failing the following test

  1. 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.

My thoughts:
I’ve tried changing my code to either display 1,2,3, or 3,2,1 by ascending or descending priority, either way, my code is being judged as wrong by the test.

Ascending or Descending + First-In-First-Out
My code can be simply be changed to ascending or descending priority by changing the comparative > operator to < in the for loop. First-in-first-out is also considered and working fine.

Test Result (Descending order)

var test = new PriorityQueue()
    test.enqueue(['Codey', 2]);
    test.enqueue(['Ken', 1]);
    test.enqueue(['Hamster', 1]);
    test.enqueue(['Nintendo', 1])
    test.enqueue(['Dreamcast', 2])

console.log(test.collection)
[ [ 'Codey', 2 ],
  [ 'Dreamcast', 2 ],
  [ 'Ken', 1 ],
  [ 'Hamster', 1 ],
  [ 'Nintendo', 1 ] ]

Any comment is appreciated, thanks.

Your enqueue method is not working. You need to test your methods with test data.

  var test = new PriorityQueue();
  test.enqueue(['David Brown', 2]);
  test.enqueue(['Jon Snow', 1]);
  var size1 = test.size();
  console.log(size1); // should display 2 but instead displays 0
1 Like

Dear Randell,

Your suggestion was very helpful.

I went back and tested my method, fixed the code and got all test working but one.

I’ve updated my code and showing test result.

Could you please take a look when you have time and let me know what you think, why it is wrong?

Thank you,

I just want to say that I finally got it to pass today.

The following is what I did.

Align items in terms of their priority, for solving this challenge, the lower number item will be placed up top, above higher number item.

eg: [Hamster, 1], [Rabbit, 2], [T-rex, 3], [Human, 4]

The wording in this question, my opinion, is somewhat confusing, but aligning items in terms of above order, from small number to large number would be correct.

Dequeue, this really got a lot of people I believe.

The dequeue should return only the current item, not its priority.
Above sentence is taken from the last sentence of the question, which asks that DO NOT RETURN THE NUMBER, ONLY ITEM VALUE!

Basically you can only return the item, see below example.

Correct: [Human]
Wrong: [Human, 4] <----- if you return value like this you wouldn’t pass.

this.dequeue = () => {
            let val = this.collection.shift()
            return val[0]
}

Return the value ONLY!

Anyhow, glad I finally got it solved!