" Trying to dequeue past the write pointer should return null and does not advance the write pointer."
My log information tell me otherwise. Write is not modified by dequeue, read is not modified when returning null. is there something i am missing ? can i have the condition the test case check again ?
Your code so far
class CircularQueue {
constructor(size) {
this.queue = new Array(size).fill(null);
this.read = 0;
this.write = 0;
this.max = size - 1;
console.log(`--------------------------------------------------------`)
console.log(`myqueue:${size} r:${this.read} w:${this.write} q:${this.queue}`)
}
print() {
return this.queue;
}
enqueue(item) {
// Only change code below this line
if(this.queue[this.write] === null){
this.queue[this.write] = item;
this.write = (this.write+1)%(this.max+1);
}
console.log(`enqueue:${item} r:${this.read} w:${this.write} q:${this.queue}`)
// Only change code above this line
}
dequeue() {
// Only change code below this line
let res = this.queue[this.read];
if (res != null) {
this.queue[this.read] = null;
this.read = (this.read+1)%(this.max+1);
}
console.log(`dequeue:${res} r:${this.read} w:${this.write} q:${this.queue}`)
return res;
// Only change code above this line
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36.
You want to add an extra test that just validates the enqueue method returns either the item or null? If so, I think that would be a good idea. I actually suggest creating two separate tests. One when it should return the item successfully enqueued and one when it should return null for unsuccessful enqueue.
To contribute, I suggest reading the following two documents: