Complete the code to push an element onto the stack using array methods.
const stack: number[] = []; stack.[1](10);
The push method adds an element to the end of the array, which simulates pushing onto a stack.
Complete the code to remove the top element from the stack using array methods.
const stack: number[] = [1, 2, 3]; const top = stack.[1]();
The pop method removes and returns the last element of the array, simulating the stack's pop operation.
Fix the error in the code that tries to access the top element of the stack without removing it.
const stack: number[] = [5, 10, 15]; const top = stack[[1]];
The top element of the stack is at the last index, which is stack.length - 1.
Fill both blanks to implement a stack class method that pushes and then pops an element.
class Stack { private items: number[] = []; push(item: number) { this.items.[1](item); } pop() { return this.items.[2](); } }
The push method adds an item to the stack, and pop removes the last item, following stack behavior.
Fill all three blanks to implement a stack peek method that returns the top element without removing it.
class Stack { private items: number[] = []; peek() { if (this.items.length === 0) return undefined; return this.items[[1]]; } size() { return this.items.[2]; } isEmpty() { return this.items.[3] === 0; } }
The peek method accesses the last element at index length - 1. The size method returns the array's length. The isEmpty method checks if length is zero.
