Skip to content

Commit 9937d62

Browse files
author
AmiyahJo
committed
fix: peek() method in stack.ts if statement
1 parent 6175f1b commit 9937d62

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

lesson_12/structs_ts/src/stack.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ export class Stack {
2424
}
2525

2626
peek(): number | null {
27-
if (this.isEmpty()) {
28-
return null;
27+
if (this.top === undefined) {
28+
throw new Error('Stack is empty');
2929
}
30-
const currentTop = this.top;
31-
return currentTop ? currentTop.val : null;
30+
return this.top.val;
3231
}
3332

3433
isEmpty(): boolean {
35-
return this.top === null;
34+
return this.top === undefined;
3635
}
3736
}

0 commit comments

Comments
 (0)