class Queue {
constructor() {
this.list = [];
this.length = 0;
}
enqueue(value) {
this.length++;
this.list.push(value);
}
dequeue() {
if (this.length === 0) return;
this.length--;
return this.list.shift();
}
peek() {
return this.list[this.list.length - 1];
}
}
Since this is a queue (aka: first in, first out), the peek method should return the first element in the list, not the last.
class Queue {
constructor() {
this.list = [];
this.length = 0;
}
enqueue(value) {
this.length++;
this.list.push(value);
}
dequeue() {
if (this.length === 0) return;
this.length--;
return this.list.shift();
}
peek() {
return this.list[0];
}
}