求助:创建一个优先队列 过不去 | Create a Priority Queue Class


#1

Tell us what’s happening:
最后一条过不去,本地测试确实是高优先级先出(优先级高的数字小),同级先进先出,测试图如下:

Your code so far


function PriorityQueue () {
    this.collection = [];
    this.printCollection = function() {
      console.log(this.collection);
    };
    this.enqueue = function(arr) {
        this.collection.push(arr);
        this.printCollection();
        return arr;
    };
    this.findMaxPriority = function() {
      if(this.collection.length==0){
        return false;
      }
      else if(this.collection.length==1){
        return 0;
      }
      else {
        var index = 0;
        for(var i=1;i<this.collection.length;i++) {
          if(this.collection[i][1]<this.collection[index][1]){
            index = i;
          }
        }
        return index;
      }
    }
    this.dequeue = function() {
      if (this.collection.length>0) {
        this.collection.splice(this.findMaxPriority(),1);
        console.log(this.collection);
        return this.collection;
      }
    };
    this.size = function() {
        return this.collection.length;
    };
    this.isEmpty = function() {
        return this.size()===0;
    };
}
var test = new PriorityQueue();
test.enqueue(["Alan",1]);
test.enqueue(["Bob",1]);
test.enqueue(["Cyber",0]);
test.enqueue(["Dylan",0]);
test.dequeue();//Cyber 出队
test.dequeue();//Dylan 出队
test.dequeue();//Alan 出队

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36.

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


#2

注意读题:

dequeue 方法应该返回元素内容,无需返回它的优先级

你返回的是 this.collection,应该返回的是类似于 "Bob" 这样的


#3

谢答,已通过。元宵快乐,病毒退散!