Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

4.1 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69a92f0b9cc04bb0d5327bb5 Implement a Stack 26 implement-a-stack

--description--

In this lab, you will implement a stack data structure using functions. A stack is a Last-In-First-Out (LIFO) data structure where elements are added and removed from the top.

Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.

User Stories:

  1. You should have an initStack function that returns an object with a collection property set to an empty array.
  2. You should have a push function that adds an element to the top of the stack.
  3. You should have a pop function that removes and returns the top element of the stack, or undefined if there isn't one.
  4. You should have a peek function that returns the top element of the stack without removing it, or undefined if there isn't one.
  5. You should have an isEmpty function that returns true if the stack contains no elements, and false otherwise.
  6. You should have a clear function that removes all elements from the stack.

Note: Most tests depend on the initStack and push functions. Implement them first, as tests for pop, peek, isEmpty, and clear require adding elements to the stack.

--hints--

You should have a push function.

assert.isFunction(push);

push function should add an element to the top of the stack.

const stack = initStack();
push(stack, 'first');
push(stack, 'second');
assert.deepEqual(stack.collection, ['first', 'second']);

You should have a pop function.

assert.isFunction(pop);

pop function should remove and return the top element of the stack.

const stack = initStack();
push(stack, 'first');
push(stack, 'second');
push(stack, 'third');
assert.strictEqual(pop(stack), 'third');
assert.strictEqual(pop(stack), 'second');

pop function should return undefined if the stack is empty.

const stack = initStack();
assert.isUndefined(pop(stack));

pop function should return falsy values correctly.

const stack = initStack();
push(stack, '');
assert.strictEqual(pop(stack), '');
assert.lengthOf(stack.collection, 0);

You should have a peek function.

assert.isFunction(peek);

peek function should return the top element of the stack without removing it.

const stack = initStack();
push(stack, 'first');
push(stack, 'second');
assert.strictEqual(peek(stack), 'second');
assert.strictEqual(peek(stack), 'second');

peek function should return undefined if the stack is empty.

const stack = initStack();
assert.isUndefined(peek(stack));

peek function should return falsy values correctly.

const stack = initStack();
push(stack, 0);
assert.strictEqual(peek(stack), 0);

You should have an isEmpty function.

assert.isFunction(isEmpty);

isEmpty function should return true for an empty stack.

const stack = initStack();
assert.isTrue(isEmpty(stack));

isEmpty function should return false for a non-empty stack.

const stack = initStack();
push(stack, 'element');
assert.isFalse(isEmpty(stack));

isEmpty function should return false when the top element is falsy.

const stack = initStack();
push(stack, false);
assert.isFalse(isEmpty(stack));

You should have a clear function.

assert.isFunction(clear);

clear function should remove all elements from the stack.

const stack = initStack();
push(stack, 'first');
push(stack, 'second');
push(stack, 'third');
clear(stack);
assert.lengthOf(stack.collection, 0);

--seed--

--seed-contents--

--solutions--

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

function print(stack) {
  console.log(stack.collection);
}

function push(stack, element) {
  stack.collection.push(element);
}

function pop(stack) {
  return stack.collection.pop();
}

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

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

function clear(stack) {
  stack.collection = [];
}