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