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: