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

17 - iterators module: Stack data structure pop() method part of code does nothing #10

Open
tiropictor opened this issue Jul 10, 2024 · 0 comments

Comments

@tiropictor
Copy link

https://github.com/bradtraversy/javascript-sandbox/blob/main/17-iterators-data-structures/08-stacks/script.js

following part of code does nothing and can be (should be?) removed / commented out:

    for (let i = this._count; i < this._items.length; i++) {
      this._items[i] = this._items[i + 1];
    }

Because it takes LAST element of array (which should be removed/popped out of array), then takes NEXT element (which apparently, doesn't exist, so it takes undefined), makes last element undefined and on next line of code this element is sliced out (and all the required job is actually done here):

this._items.length = this._count;

Whole this loop runs exactly ONCE (because its starting and ending points are next to each other) and doesn't really "shift left" anything.

It seems more like part of code for Queues data structure, but in wrong conditions..

Nice course anyway, thank you!

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

No branches or pull requests

1 participant