Basic Data Structure Explanation

Hey guys,
I am doing this Priority Queue challenge
challenge

and below is my code so far:

Can someone tell me why it doesn’t work?

function PriorityQueue () {
  this.collection = [];
  this.printCollection = function() {
    console.log(this.collection);
  };
  // Only change code below this line
  this.enqueue = (elem) => {
    if(this.isEmpty()) {
      this.collection.push(elem)
    }
    let found_idx = -1;
    for(var i = 0; i < this.collection.length; i++){
      if(this.collection[i][1] >= elem[1]){
        this.collection.push(elem)
        found_idx = i
      }
    }
    if (found_index === -1) {
      this.collection.push(elem);
    } else {
      this.swap(found_idx, this.collection.length - 1)
    }
    
  } 
  this.dequeue = () => {
    if (!this.isEmpty()) {
      return this.collection.shift()[0];
    } else {
      return "The queue is empty.";
    }
  }  
  // item's name 
  this.front = () => {
    return this.collection[0][0]
  } 
  this.size = () => {
    return this.collection.length
  } 
  this.isEmpty = () => {
    return this.collection.length < 0
  } 

  this.swap = (st, nd) => {
    [this.collection[st], this.collection[nd]] = [this.collection[nd], this.collection[st]]
  }
    // Only change code above this line
}

front

method should return the correct item at the front of the queue as items are enqueued and dequeued.

isEmpty

method should return

true

when the queue is empty. The priority queue should return items with a higher priority before items with a lower priority and return items in first-in-first-out order otherwise.

Try this:

function PriorityQueue() {

  this.collection = [  ];

  this.printCollection = function () {

    console.log(this.collection);

  };

  // Only change code below this line

  this.enqueue = function (newitem) {

    if (this.isEmpty()) {

      return this.collection.push(newitem);

    }

    this.collection = this.collection.reverse();

    var found_index = this.collection.findIndex(function (item) {

      return newitem[1] >= item[1];

    });

    if (found_index === -1) {

      this.collection.push(newitem);

    } else {

      this.collection.splice(found_index, 0, newitem);

    }

    this.collection = this.collection.reverse();

  };

  this.dequeue = function () {

    if (!this.isEmpty()) {

      return this.collection.shift()[0];

    } else {

      return "The queue is empty.";

    }

  };

  this.size = function () {

    return this.collection.length;

  };

  this.front = function () {

    return this.collection[0][0];

  };

  this.isEmpty = function () {

    return this.size() > 0 ? false : true;

  };

  // Only change code above this line

}

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

2 Likes

Thank you for the suggestion :slight_smile: