Create a Priority Queue Class (thought my code should be fine)

Tell us what’s happening:

Your code so far


function PriorityQueue () {
    this.collection = [];
    this.printCollection = function() {
      console.log(this.collection);
    };
    // Only change code below this line
  this.enqueue = (arr) => {
    if(this.collection.length === 0){
      return this.collection.push(arr)
    }
    for(let i = 0; i < this.collection.length; i++){
      if(this.collection[i][1] > arr[1]) {
        return this.collection.splice(i, 0, arr)
      }
    }
  }
  this.dequeue = () => {
    if(this.collection.length === 0){
      return null
    }
    let removedElement = this.collection.shift()
    return removedElement[0]
  }
  this.size = () => {
    return this.collection.length  
  }
  this.front = () => {
    return this.collection[0][0]
  }
  this.isEmpty = () => {
    return this.collection.length === 0
  }
  
    // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

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

It took a little, but I think I figured out where the problem is. What happens if you’re adding another entry to the queue whose priority is the same as the last one in the queue? As an example, the test suite adds a few members to the queue:

['David Brown', 2]
['A', 3]
['B', 3]

When that first member is enqueued, the queue is empty, so that logic works. When the second member is added, its priority is greater than the last in the queue (I mean that ['David Brown', 2] has a lower priority than ['A', 3]). Where is the logic to add a member to the queue in the case of an equal or greater priority?

I added a few console.log() to see what was going on, a practice I strongly recommend. That was what showed me that things weren’t going quite right. And a gotcha on that, when you console.log() the value of this.collection, EVERY INSTANCE SHOWN will change in the console as this.collection updates. It stumped the CRAP out of me. Instead, use something like

console.log("Current collection: ", [...this.collection] );
// The above spreads the array, then rejoins it, effectively displaying it as a new, static, array.

ALSO NOTE You really should read the MDN docs on the various functions you’re using. Specifically, check out what each return. For example, when you enqueue and you return this.collection.splice(i, 0, arr), the return value is ANY DELETED ELEMENTS. If none are deleted, it returns an empty array. View the Array.splice() docs to see what that means. And specifically, in that doc, check out return values. I would strongly recommend you do your array manipulation, and as a separate step, do the return value. Mixing them is going to give you unexpected results.

Hope that helps!

Another glaring example of reading the MDN docs is, when you use Array.push(), the returned value from that is the length of the array after the push. I don’t think, when you enqueue a record, that is what you intend to return.

Thanks a lot for your help!!

Hey @hlhnicola, couple remarks from my side:

  1. Consider this code:
this.dequeue = () => {
   if(this.collection.length === 0){
     return null
   }
   let removedElement = this.collection.shift()
   return removedElement[0]
 }

If you would be ok to return undefined in the case of empty collection (which you probably should), then your code could be simplified to this:

this.dequeue = () => this.collection.shift();
  1. Consider this code:
this.enqueue = (arr) => {
    if(this.collection.length === 0){
      return this.collection.push(arr)
    }
    for(let i = 0; i < this.collection.length; i++){
      if(this.collection[i][1] > arr[1]) {
        return this.collection.splice(i, 0, arr)
      }
    }
  }

you check two conditions:
A. If collection is empty => push
B. If I find element with higher priority => insert before

Now, what if you won’t find the element with higher priority? In other words, what if arr[1] is greater than all existing priorities? (HINT: It could be fixed in a very smart way, but I’ll let you try to guess first)

Good luck!