Create a Priority Queue Class -- dequeuing method + unable to pass tests

Tell us what’s happening:

Hi all. I’m hoping to clean up my code once I get it functional – but for some reason, my dequeuing method won’t work. I even tried using the Javascript “shift()” method and that doesn’t seem to remove it when I do “this.printCollection”. Any advice?

Also, my tests aren’t passing for my isEmpty or size method also…so confused!
Thanks in advance.

Your code so far


function PriorityQueue () {
    this.collection = [];
    this.printCollection = function() {
      console.log(this.collection);
    };
    // Only change code below this line
    this.enqueue = function(value) {
      var arrLength = this.collection.length;
      if (this.collection.length === 0) {
        this.collection[0] = value;
      } else {
        if (value[1] >= this.collection[arrLength - 1][1]) {
        this.collection[arrLength] = value;
        } else {
          for (var i = 0; i < arrLength; i++) {
            if (value[1] < this.collection[i][1]) {
              this.collection.splice(i, 0, value);
              break;
            }    
          }
        }
      }
    };
    this.dequeue = function() {
      var dequeuedItem = this.collection[0][0];
      delete this.collection[0];
      return dequeuedItem;
    };
    this.size = function() {
      return this.collection.length;
    };
    this.isEmpty = function() {
      this.collection.length < 1;
    };
    // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-priority-queue-class

  1. isEmpty isn’t passing because you forgot to return
  2. dequeue (and enqueue) aren’t passing because you aren’t tracking size as items are added or removed from this.collection, as it says in the direcitons. Run this.size() after you modify this.collection and before your return

Hope this helps :grin: