Test code checks for:
test.enqueue(100) === 100 &&
test.dequeue() === 100;
but, enqueue isn’t supposed to return anything. I think this is a bug. My code fails last test : Trying to dequeue past the write pointer returns null
and does not advance the write pointer.
Your code so far
class CircularQueue {
constructor(size) {
this.queue = [];
this.read = 0;
this.write = 0;
this.max = size - 1;
while (size > 0) {
this.queue.push(null);
size--;
}
}
print() {
return this.queue;
}
enqueue(item) {
// Only change code below this line
if( (this.queue[this.write]) == null){
this.queue.splice(this.write,1,item);
this.write++;
this.write = this.write % this.queue.length;
}
else return null;
// Only change code above this line
}
dequeue() {
// Only change code below this line
if((this.queue[this.read]) == null){
return null;
}
else {
let item = this.queue.splice(this.read,1,null);
this.read++;
this.read = this.read % this.queue.length;
return item[0];
}
// Only change code above this line
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-circular-queue