Skip to content

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

@tiropictor

Description

@tiropictor

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions