diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index 15e6a23b51e..487e57279e0 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -5606,6 +5606,12 @@ "In this lab you will implement common linked list operations such as insertion, deletion, and traversal." ] }, + "lab-implement-a-stack": { + "title": "Implement a Stack", + "intro": [ + "In this lab, you will implement a stack data structure using functions." + ] + }, "lab-implement-a-queue": { "title": "Implement a Queue", "intro": [ diff --git a/curriculum/challenges/english/blocks/lab-implement-a-stack/69a92f0b9cc04bb0d5327bb5.md b/curriculum/challenges/english/blocks/lab-implement-a-stack/69a92f0b9cc04bb0d5327bb5.md new file mode 100644 index 00000000000..67c859cc551 --- /dev/null +++ b/curriculum/challenges/english/blocks/lab-implement-a-stack/69a92f0b9cc04bb0d5327bb5.md @@ -0,0 +1,190 @@ +--- +id: 69a92f0b9cc04bb0d5327bb5 +title: Implement a Stack +challengeType: 26 +dashedName: 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 a `push` function that adds an element to the top of the stack. +1. You should have a `pop` function that removes and returns the top element of the stack, or `undefined` if there isn't one. +1. You should have a `peek` function that returns the top element of the stack without removing it, or `undefined` if there isn't one. +1. You should have a `isEmpty` function that returns `true` if the stack contains no elements, and `false` otherwise. +1. You should have a `clear` function that removes all elements from the stack. + +**Note:** Most tests depend on the `push` function. Implement `push` first, as tests for `pop`, `peek`, `isEmpty`, and `clear` require adding elements to the stack. + +# --hints-- + +You should have a `push` function. + +```js +assert.isFunction(push); +``` + +`push` function should add an element to the top of the stack. + +```js +const stack = initStack(); +push(stack, 'first'); +push(stack, 'second'); +assert.deepEqual(stack.collection, ['first', 'second']); +``` + +You should have a `pop` function. + +```js +assert.isFunction(pop); +``` + +`pop` function should remove and return the top element of the stack. + +```js +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. + +```js +const stack = initStack(); +assert.isUndefined(pop(stack)); +``` + +`pop` function should return falsy values correctly. + +```js +const stack = initStack(); +push(stack, ''); +assert.strictEqual(pop(stack), ''); +assert.lengthOf(stack.collection, 0); +``` + +You should have a `peek` function. + +```js +assert.isFunction(peek); +``` + +`peek` function should return the top element of the stack without removing it. + +```js +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. + +```js +const stack = initStack(); +assert.isUndefined(peek(stack)); +``` + +`peek` function should return falsy values correctly. + +```js +const stack = initStack(); +push(stack, 0); +assert.strictEqual(peek(stack), 0); +``` + +You should have an `isEmpty` function. + +```js +assert.isFunction(isEmpty); +``` + +`isEmpty` function should return `true` for an empty stack. + +```js +const stack = initStack(); +assert.isTrue(isEmpty(stack)); +``` + +`isEmpty` function should return `false` for a non-empty stack. + +```js +const stack = initStack(); +push(stack, 'element'); +assert.isFalse(isEmpty(stack)); +``` + +`isEmpty` function should return `false` when the top element is falsy. + +```js +const stack = initStack(); +push(stack, false); +assert.isFalse(isEmpty(stack)); +``` + +You should have a `clear` function. + +```js +assert.isFunction(clear); +``` + +`clear` function should remove all elements from the stack. + +```js +const stack = initStack(); +push(stack, 'first'); +push(stack, 'second'); +push(stack, 'third'); +clear(stack); +assert.lengthOf(stack.collection, 0); +``` + +# --seed-- + +## --seed-contents-- + +```js +``` + +# --solutions-- + +```js +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 = []; +} +``` diff --git a/curriculum/structure/blocks/lab-implement-a-stack.json b/curriculum/structure/blocks/lab-implement-a-stack.json new file mode 100644 index 00000000000..79d042be18b --- /dev/null +++ b/curriculum/structure/blocks/lab-implement-a-stack.json @@ -0,0 +1,14 @@ +{ + "isUpcomingChange": true, + "dashedName": "lab-implement-a-stack", + "helpCategory": "JavaScript", + "blockLayout": "link", + "blockLabel": "lab", + "usesMultifileEditor": true, + "challengeOrder": [ + { + "id": "69a92f0b9cc04bb0d5327bb5", + "title": "Implement a Stack" + } + ] +} diff --git a/curriculum/structure/superblocks/javascript-v9.json b/curriculum/structure/superblocks/javascript-v9.json index 3851bc4edc6..eab9b8d2447 100644 --- a/curriculum/structure/superblocks/javascript-v9.json +++ b/curriculum/structure/superblocks/javascript-v9.json @@ -304,6 +304,7 @@ "blocks": [ "lecture-working-with-common-data-structures-js", "lab-linked-list-operations", + "lab-implement-a-stack", "lab-implement-a-queue", "review-data-structures-js", "quiz-data-structures-js"