Skip to content

Commit

Permalink
fix: peek() method in stack.ts if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
AmiyahJo committed Oct 23, 2024
1 parent 6175f1b commit 9937d62
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lesson_12/structs_ts/src/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 9937d62

Please sign in to comment.