Dequeue algorithm is not working. please help

I have been trying to perform dequeue operation using for loop but it’s not working while the enqueue is working at my end.

//queue class
class Queue {
  //// Array is used to implement queue
  constructor() {
    this.items = [];
    // Functions to be implemented
    // enqueue(item)
    // dequeue()
    // isEmpty()
    // printQueue()
  }
  //this.size = 0;

  // enqueue function
  enqueueCustomMethod(element) 
 

  {
    this.items[this.size] = element;
    console.log(`${element} added `);
    this.size += 1;
    return this.size - 1;
    // adding element to the queue using custom method
    //this.items.push(element);
  }

dequeueCustomMethod() {
    
    for (let i = 0; i < this.items.length; i++) {
      this.items[i - 1] = this.items[i];
      //console.log(`${this.items} deleted `);
    }

    this.items[this.items.length - 1] = 0;
  }
let myQueue = new Queue();
// Adding elements to the queue
// queue contains [10, 20, 30, 40, 50]
myQueue.enqueueCustomMethod(10);
myQueue.enqueueCustomMethod(20);
myQueue.enqueueCustomMethod(30);
myQueue.enqueueCustomMethod(40);
myQueue.enqueueCustomMethod(50);
myQueue.enqueueCustomMethod(60);
myQueue.enqueueCustomMethod(70);

// removes 10 from the queue
// queue contains [20, 30, 40, 50, 60]
console.log(myQueue.dequeueCustomMethod());

What are you trying to accomplish there? The first loop you have this.items[-1] = this.items[0];.

I think you’ll need to use a new array and add items to it if you want to continue using a for loop. Does the challenge discourage using push, pop and other array methods?

the challenge is to create a logic of shift method ( dequeue in stack) without using shift itself. How would I do that?

Do you need to return the item you remove from the array?

Nope. I don’t need to return the item i remove from the array right now.

Which challenge are you working on? Please add a link.

I have got an assignment from my bootcamp intructor to perform queues operations (enqueue, dequeue) on an array. As I share my complete my code above.

How did you verify that the enqueueCustomMethod method is working? I think you should try using console.log in both methods to verify the contents of this.items after running.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.