Circular Queue in Javascript

This should be working. I have verified my variables and they are not passing the tests. What is going on?

  **Your code so far**

class CircularQueue {
constructor(size) {

  this.queue = [];
  this.max = size - 1;
  this.size = size
  this.length = 0
  this.front = 0
  this.back = -1
  
  while (size > 0) {
    this.queue.push(null);
    size--;
  }
}

print() {
  return this.queue;
}


enqueue(item) {
  if(this.length>=this.size){
   this.back++
   this.item[this.back & this.size] = item
   this.length++ 
  }
}
isEmpty(){
  return queue.length===0
}

dequeue() {
  if (this.isEmpty()){
    const value=this.getFront()
    this.item[this.front%this.size]=null
    this.front++
    this.length--
    return value
  }
}
  getFront(){
    if (this.isEmpty()){
      return this.item[this.front % this.size]

}

  }
}
  **Your browser information:**

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

Challenge: Create a Circular Queue

Link to the challenge:

What do the failing tests say?

1 Like

The enqueue method should add items to the circular queue.

You should not enqueue items past the read pointer.

The dequeue method should dequeue items from the queue.

After an item is dequeued, its position in the queue should be reset to null .

Trying to dequeue past the write pointer should return null and does not advance the write pointer.

Edit: nevermind. The whole code didn’t load for me when I looked at it before.

1 Like

You’ve changed some of the code that wasn’t meant to be changed. You were only supposed to add code in the enqueue and dequeue methods but you have removed some of the properties in the constructor and added new ones. I would suggest you click the “Reset All Code” button and start over fresh and only add code to the two methods that need it. You can definitely solve this without changing anything else. Personally, I think you have over complicated this by all the extra changes you made.

3 Likes
class CircularQueue {

  constructor(size) {

    this.queue = [];

    this.read = 0;//puntero inicial

    this.write = 0;//puntero final

    this.max = size - 1;

    while (size > 0) {

      this.queue.push(null);

      size--;

    }

  }

  print() {

    return this.queue;

  }

  enqueue(item) {

    if(this.queue[this.write]==null){

        this.queue[this.write]=item

        this.write+=1

     }

     if(this.write>this.max){

       this.write=0

       

     }

    return null

  }

  dequeue() {

    if(this.queue[this.read]==1||this.queue[this.read]==2||this.queue[this.read]==3||this.queue[this.read]==4||this.queue[this.read]==5||this.queue[this.read]==6||this.queue[this.read]==7||this.queue[this.read]==8||this.queue[this.read]==9||this.queue[this.read]==0){

      this.queue[this.read]=null

       this.read+=1 

        

    }

    if(this.read>this.max){

      this.read=0

      

    }

  return null

  }

}

let a = new CircularQueue(3)

a.enqueue(1)

a.enqueue(2)

a.enqueue(4)

a.dequeue()

console.log(a)

I am using the function and I think is working, but it is not passing the tests. What is happening?

For starters, your enqueue and dequeue methods are always returning null. The instructions say:

“The enqueue method should return the item you enqueued if it is successful; otherwise it will return null .”

“When you dequeue an item, that item should be returned. If you cannot dequeue an item, you should return null .”

1 Like

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