Create a Priority Queue Class

Create a Priority Queue Class
0

#1

Tell us what’s happening:
Hi guys! Soo I wrote some code for the Priority Queue challenge and after I run it I’m getting green light for all the requirements except:
Your Queue class should have a dequeue method.
Your Queue class should have a size method.
Your Queue class should have an isEmpty method.
and I don’t get it, because evidently I have those methods in my class.
thx in advance and sorry for my bad english

Your code so far


function PriorityQueue () {
    this.collection = [];
    this.printCollection = function() {
      console.log(this.collection);
    };
    // Only change code below this line
    this.enqueue=function(item){
        if(this.collection.length===0) {
        this.collection.push(item);
      }
      else if(this.collection.length===1){
        if(item[1]>=this.collection[0][1]){
          this.collection.push(item);
        }
        else if(item[1]<this.collection[0][1]){
          this.collection.unshift(item);
        }
      }
     else if(this.collection.length>1){
        var check=false;
        for(let i=0;i<this.collection.length;i++){
            if(this.collection[i][1]>item[1]){
              if(i===0){
                check=true;
                this.collection.unshift(item);
                break;
              }
              else {
                check=true;
                var sliced=this.collection.slice(0,i);
                var slicedend=this.collection.slice(i,this.collection.length);
                sliced.push(item);
                this.collection=sliced.concat(slicedend);
                break;
              }
            }
        }
        if(!check){
                this.collection.push(item);
        }
    };


    this.dequeue = function(){
        return this.collection.shift()[0];
    };
    this.size = function(){
        return this.collection.length;
    };
    this.isEmpty=function(){
        return this.collection.length===0?true:false;
    };
    this.front=function(){
        return this.collection[0];
    };
    }
    // Only change code above this line
}

Your browser information:

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

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


Data Structures: Create a Priority Queue Class fail
#2

You’re missing a closing bracket somewhere around here:

    if(!check){
                this.collection.push(item);
        }
};

And there’s an extra bracket here, which means the brackets match, but are in the wrong place (everything is inside the enqueue function):

    this.front=function(){
        return this.collection[0];
    };
    }
    // Only change code above this line
}

Your enqueue function is incredibly complex: for a start, if you’ve got more than two levels of nesting you really need to refactor, it’s very difficult to keep track of things.


#3

oh yeah, silly me.
also i’ll try to write simpler and cleaner code in the future. thx again


#4

Your dequeue method it is such an overkill.
here is a suggestion for refactoring

   enqueue(item){
            let element = item[0], priority = item[1], check = false;

          for(let i = 0; i < this.collection.length; i++){
            if(this.collection[i][1] > priority){
                this.collection.splice(i,0,item);
                check = true;
                break;
            } 
          }
          if(!check)
              this.collection.push(item);
      }