CodeNot attempted
Generic Stack class
Difficulty: easy
Build Stack<T> with push, pop and peek (both T | undefined), a size getter and isEmpty(). Keep the storage truly private (#field), and make push only accept T.
Show reference solution
export class Stack<T> { #items: T[] = []; // # makes it really private, so it doesn't show up on the instance
push(item: T): void { this.#items.push(item); }
pop(): T | undefined { return this.#items.pop(); }
peek(): T | undefined { return this.#items[this.#items.length - 1]; }
get size(): number { return this.#items.length; }
isEmpty(): boolean { return this.#items.length === 0; }}
Ctrl Enter