"Create a Priority Queue Class Correct " solution fails tests

The code solution I entered in the “Coding interview Prep”, section Data Structure, challenge “Create a Priority Queue Class” seems to have some failed validation tests.

The code entered is:

function PriorityQueue () {
  this.collection = [];
  this.printCollection = function() {
    console.log(this.collection);
  };
  // Only change code below this line
  this.enqueue = function(elem) {
    if (this.collection.length == 0) {
      this.collection.push(elem);
    } else { 
      added = false;
      let size = this.collection.length;
      for (let c = 0; c < size; c++) {
        if (this.collection[c][1] > elem[1]) {
          this.collection.splice(c, 0, elem);
          added = true;
          break;
        }
      }
      if (!added)
        this.collection.push(elem);
      }
  };
  
  this.dequeue = function() {
    if (this.collection.length == 0) {
      return null;
    } else {
      let elem = this.collection[0];
      this.collection.splice(0, 1);
      return elem;
    }
  };
  
  this.size = function() {
    return this.collection.length;
  };
  
  this.front = function() {
    return this.collection[0][0];
  };
  
  this.isEmpty = function() {
    return this.collection.length == 0;
  };
  // Only change code above this line
}

Unless I am missing something, which does not seem to be the case as I have tested the code locally, it seems there is something wrong with the validation tests.

I have tried in different browsers, Chrome and Firefox, but it fails in both with the following message:

can you post a link to the challenge?

Sorry, I did not post the link previously. Here it is:

Hi Randell,
thanks for your time taking a look into it.
Regarding the dequeue and front methods requirement, it seems the challenge text should be bit more clear if the ambiguity was cleared off. I agree with you that the method dequeue returns the element with the name and its priority, but it was due to some confusion on the challenge text requirements: (image below)

hence it would be resolved with a simple change on method dequeue from:

let elem = this.collection[0];

to

let elem = this.collection[0][0];

in order to retrieve the item’s name only and not the whole item. The front method was already returning only the item’s name.

The only thing that did not shout out at my chrome’s console neither in firefox’s console, in a local test was the flag (boolean) variable named added. Regarding the challenge’s validation It was the culprit. At the method enqueue changing it from:

added = false;

to

let added = false;

did the trick.

Thanks Randell.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.