Help with Create a Priority Queue Class

Help with Create a Priority Queue Class
0.0 0

#1

Tell us what’s happening:
Can’t figure out where i am going wrong with my code for priority queue!
Please help ?

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) {
      let pos = 0;

      if(this.collection.length === 0) {
        this.collection.push(item);
      } 
      else if(this.collection.length===1) {
        
        if(item[1] < this.collection[0][1]) {
          this.collection.unshift(item);
        } else {
          this.collection.push(item);
        }
      }       
      else {
        if(item[1] < this.collection[0][1]) {
          this.collection.unshift(item);
        }
        let pos = 0;
        while(this.collection[pos][1]<=item[1] && pos<this.collection.length) {
          pos++;
        }
        this.collection.splice(pos,0,item);
      }
    }
    this.dequeue = function() {
      return this.collection.shift()[0];
    }
    this.size = function() {
      return this.collection.length;
    }
    this.front = function() {
      return this.collection[0];
    }
    this.isEmpty = function() {
      return this.collection.length == 0;
    }
    // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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


#2

For one mistake i have seen i repeated instatiation of my pos variable but that shouldnt affect rest of code i think.


#5

Ugh! Ok, sorry I had to delete my previous solutions. I kept fixing it the wrong way. lol

The issue is this line right here.

while(this.collection[pos][1]<=item[1] && pos<this.collection.length)

this.collection[pos][1] will throw an error because if the var pos is greater than the collection length, it will try to access an undefined value. This error shows that

Cannot read property ‘1’ of undefined

My previous solution only worked because I used the array.forEach method, which won’t go past the array length.

Change the logic around to first check if var pos < this.collection.length.

while(pos < this.collection.length && this.collection[pos][1] <= item[1])

The && operator will always evaluate the first condition, and only evaluate the second if the first fails. It won’t check both and then evaluate.


#6

you rock thank you very much was getting frustrated ended up changing correct parts for nothing!!!


#7

You’re welcome. It was definitely a tricky bug.

Since you’re iterating arrays, below I show ways to protect against accessing invalid indices. But it’s just a preference. They will all return the same result.

// forEach
let pos = 0;
this.collection.forEach(coll => {
  if (coll[1] <= item[1]) pos++
})

// reduce
const pos = this.collection.reduce((total, coll) => {
  if (coll[1] <= item[1]) total++
  return total
}, 0)

// for...of
let pos = 0
for (const coll of this.collection) {
  if (coll[1] <= item[1]) total++
}

All except the for...of will also give you access to the index value if you need it.