From 9937d6244260bdd96bbab6abf50a5b634503a8b7 Mon Sep 17 00:00:00 2001 From: AmiyahJo Date: Wed, 23 Oct 2024 14:39:49 +0000 Subject: [PATCH] fix: peek() method in stack.ts if statement --- lesson_12/structs_ts/src/stack.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index 1f51ea4e..33d88e96 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -24,14 +24,13 @@ export class Stack { } peek(): number | null { - if (this.isEmpty()) { - return null; + if (this.top === undefined) { + throw new Error('Stack is empty'); } - const currentTop = this.top; - return currentTop ? currentTop.val : null; + return this.top.val; } isEmpty(): boolean { - return this.top === null; + return this.top === undefined; } }