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
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” But a mistake, certainly.
Do you think it’s a good idea to report it?
Yea. That’s why I provided the GitHub link
Thanks !@JeremyLT
opened 10:51AM - 01 Jun 21 UTC
scope: curriculum
status: waiting triage
type: bug
**Affected page**
https://www.freecodecamp.org/learn/coding-interview-prep/data… -structures/create-a-stack-class
**Forum post**
https://forum.freecodecamp.org/t/create-a-stack-class-a-bug-in-tests-required-implementation-of-peek-method/462776
**The problem**
When solving the challenge, the code below passes the tests:
```js
this.peek = function () {
return collection[0];
};
```
**Expected behaviour**
The tests should fail for anything other than the correct solution, which is:
```js
this.peek = function () {
return collection[collection.length - 1];
};
```
**Proposed solution**
In the test case, push one more element to the stack:
```js
assert(
(function () {
var test = new Stack();
test.push('PSY44');
test.push('CS50');
return test.peek() === 'CS50';
})()
);
```
**System (please complete the following information):**
- Device: MacBook Pro
- OS: macOS 11.3.1
- Browser: chrome
- Version: 90
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.
system
Closed
December 2, 2021, 1:32am
10
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.