Stack Pop Bug
Spot the bug in a simple array-based stack implementation.
Codejavascript
class Stack {
constructor() {
this.items = [];
}
push(value) {
this.items.push(value);
}
pop() {
return this.items.shift();
}
peek() {
return this.items[this.items.length - 1];
}
}
const s = new Stack();
s.push(1);
s.push(2);
s.push(3);
console.log(s.pop());This stack is supposed to be LIFO, but pop() misbehaves. What is the bug?