Implement a Stack - Tests 2, 6, and 16 keep failing despite correct implementation

I’m stuck on the “Implement a Stack” lab. Tests 2, 6, and 16 keep failing even though my code seems correct.

Tests failing:

    1. push function should add an element to the top of the stack
    1. pop function should return falsy values correctly
    1. clear function should remove all elements from the stack

My code:

function initStack() {
  return [];
}

function push(stack, item) {
  stack[stack.length] = item;
}

function pop(stack) {
  if (stack.length === 0) return undefined;
  const removed = stack[stack.length - 1];
  stack.length--;
  return removed;
}

function peek(stack) {
  if (stack.length === 0) return undefined;
  return stack[stack.length - 1];
}

function isEmpty(stack) {
  return stack.length === 0;
}

function clear(stack) {
  while (stack.length > 0) {
    pop(stack);
  }
}

What I’ve tried:

Using stack.push(item) and stack.pop() directly

Using a class with a collection array

Returning undefined vs not returning anything

Different ways to clear the stack

What happens:
All other tests pass except these three. The code works correctly when I test it manually.

Has anyone else encountered this issue with this lab? Is there something specific the validator is looking for that I’m missing?

Thanks in advance for any help!

hi, can you include a link to the lab?

Hi, sure!

there is a bug in this lab. You can skip it if you want until it is fixed.
Basically they are expecting the stack to be an object which contains an array.

Thanks for your prompt response!

I did that, and while working with the following lab : Implementing Queues, I came back and implemented a similar proposal and finally it worked. I shared the code :

mod edit: code removed

Regards,

no problem.

I removed the shared code to allow others to work on these labs without any spoilers from the forum.

thanks for your understanding