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:
- You should have an
initStackfunction that returns an object with acollectionproperty set to an empty array. - You should have a
pushfunction that adds an element to the top of the stack. - You should have a
popfunction that removes and returns the top element of the stack, orundefinedif there isn't one. - You should have a
peekfunction that returns the top element of the stack without removing it, orundefinedif there isn't one. - You should have an
isEmptyfunction that returnstrueif the stack contains no elements, andfalseotherwise. - You should have a
clearfunction 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 = [];
}