Create a Stack Class: a bug in tests / required implementation of peek() method

Hi, I’ve found a serious mistake in this excercise:

The specification requires us to create

a peek method that looks at the top element in the stack

whereas the implementation expects this method to return the bottom-most element (collection[0])

According to wikipedia article on peek:

It thus returns the same value as operations such as “pop” or “dequeue”, but does not modify the data.
Peek (data type operation) - Wikipedia

So, the correct implementation of peek() would be:

this.peek = function () {
        return collection[collection.length - 1]
    }

I think this should be reported as an issue. I’need help with this :slight_smile:

To what implementation expecting bottom-most element do you refer to?

Hi, I am not sure if I understand your question…
The implementation below passes the tests:

this.peek = function () {
  return collection[0];
};

While I argue it shouldn’t - peek() shoud return the same element as pop() does , but without mutating the data.
So, this should be the correct implementation:

this.peek = function () {
  return collection[collection.length - 1];
};

I wouldn’t call it a “serious mistake”.
Guide post solution:

function Stack() {
  const collection = [];
  this.print = function() {
    console.log(collection);
  };
  this.push = function(val) {
    return collection.push(val);
  };
  this.pop = function() {
    return collection.pop();
  };
  this.peek = function() {
    return collection[collection.length - 1];
  };
  this.isEmpty = function() {
    return collection.length === 0;
  };
  this.clear = function() {
    collection.length = 0;
  };
}

It is intended that the “top” of the stack is the end of the array in this solution.

It is more of a small flaw in the test cases. I’d guess that peak is being called with only one item in the stack for that test case but it should be called with at least two items in the stack.


Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

Ok, agreed, not a “serious mistake” :slight_smile: But a mistake, certainly.
Do you think it’s a good idea to report it?

Yea. That’s why I provided the GitHub link :wink:

Thanks !@JeremyLT :white_check_mark:

So, should I wait now until it is acknowledged to be really an issue or can I proceed with a PR right away?

1 Like

It looks like you should be good to make a PR now.

Done! :slight_smile: