Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding validation for peek and returning last element from array with… #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gautam-paladiya
Copy link

…out calculating length

@sudheerj
Copy link
Owner

@gautam-paladiya Thanks for your PR. Let's use a standway to access the array item.

peek() {
// Check if the stack is empty
if (this.isEmpty()) {
throw new Error("Stack Underflow: Cannot peek from an empty stack");
}
// Return the top most element from the stack without removing it
return this.array[this.array.length - 1];
}

@gautam-paladiya
Copy link
Author

Accepted please go ahead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants