Implement a Stack, test 2

Tell us what’s happening:

“Implement a Stack” lab, test 2 has never passed, it always says:
push function should add an element to the top of the stack.

My code:
const stack = { top: null };
function push(element) {
const node = { element, next: stack.top };
stack.top = node;
}

What code does this test expect?

Your code so far

const stack = { top: null };

function push(element) {
  const node = { element, next: stack.top };
  stack.top = node;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Implement a Stack - Implement a Stack

a stack is a list of things.
So what type of data structure should it be?

Welcome to the forum @yury12031975 ,

I suggest you move on to another challenge. I just created a GitHub issue for this challenge, which currently is not passing tests because they rely on a function that creates an anonymous object not mentioned in the instructions.

Happy coding!

Modeling the Stack as an object containing an array of data worked fine for me.
The Clear function passed only when I forced the array.length =0. Assigning an empty array and pop/shift all elements weren’t accepted!

I did a similar implementation using my own internal node structure, but this lab’s verification steps is verifying the internal data structure instead of verifying the output of the functions. The internal data structure it is expecting is an array so you can’t use nodes.

The user story is lacking.. It doesn’t mention to use an array as an internal data structure. It also doesn’t mention that you need to create an initStack() method, which you do need to implement.

After skipping to the next Lab - Implement a Queue and seeing it’s functions given to the students, it looks like the critical piece for this lab to create a Stack is the following code that should be given to students as well.

function initStack() {
  return { collection: [] };
}
4 Likes

I passed all tests.
This lab missed an important information, the stack structure is {collection: []}. All the operation is based on the structure.

Thank you, you’re right. Based on the conditions you provided, I have passed all the tests.

Thanks this helped me complete the assignment.

function initStack() {
return { collection: [] };
}