diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index 7c0485a8ad4..76f8fa1747e 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -2388,7 +2388,12 @@ ] }, "nixz": { "title": "262", "intro": [] }, - "zywg": { "title": "263", "intro": [] }, + "lab-stack-class": { + "title": "Build a Stack Class", + "intro": [ + "For this lab, you will build a stack class using JavaScript." + ] + }, "lab-linked-list-class": { "title": "Build a Linked List Class", "intro": [ diff --git a/client/src/pages/learn/front-end-development/lab-stack-class/index.md b/client/src/pages/learn/front-end-development/lab-stack-class/index.md new file mode 100644 index 00000000000..412bdb15925 --- /dev/null +++ b/client/src/pages/learn/front-end-development/lab-stack-class/index.md @@ -0,0 +1,9 @@ +--- +title: Introduction to the Build a Stack Class +block: lab-stack-class +superBlock: front-end-development +--- + +## Introduction to the Build a Stack Class + +For this lab, you will build a stack class using JavaScript. diff --git a/curriculum/challenges/_meta/lab-stack-class/meta.json b/curriculum/challenges/_meta/lab-stack-class/meta.json new file mode 100644 index 00000000000..74526c63fb3 --- /dev/null +++ b/curriculum/challenges/_meta/lab-stack-class/meta.json @@ -0,0 +1,11 @@ +{ + "name": "Build a Stack Class", + "isUpcomingChange": true, + "usesMultifileEditor": true, + "blockType": "lab", + "dashedName": "lab-stack-class", + "order": 263, + "superBlock": "front-end-development", + "challengeOrder": [{ "id": "587d8250367417b2b2512c5f", "title": "Build a Stack Class" }], + "helpCategory": "JavaScript" +} diff --git a/curriculum/challenges/english/25-front-end-development/lab-stack-class/587d8250367417b2b2512c5f.md b/curriculum/challenges/english/25-front-end-development/lab-stack-class/587d8250367417b2b2512c5f.md new file mode 100644 index 00000000000..7d61e6ec7b1 --- /dev/null +++ b/curriculum/challenges/english/25-front-end-development/lab-stack-class/587d8250367417b2b2512c5f.md @@ -0,0 +1,165 @@ +--- +id: 587d8250367417b2b2512c5f +title: Build a Stack Class +challengeType: 14 +dashedName: build-a-stack-class +--- + +# --description-- + +A stack is a data structure that stores an ordered collection of elements. It follows the *Last-In-First-Out* principle, where the last element inserted is removed first. + +**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab. + +**User Stories:** + +1. You should define a `Stack` class that has a `collection` property initialized to an empty array. You'll use this array to mimic a stack. +1. The `Stack` class should have a `push` method that adds an item to the top of the stack. +1. The `Stack` class should have a `pop` method that removes and returns the element on the top of the stack. +1. The `Stack` class should have a `peek` method that returns the element on the top of the stack. +1. The `Stack` class should have an `isEmpty` method that returns `true` if the stack is empty, and `false` otherwise. +1. The `Stack` class should have a `clear` method that empties the stack. + +# --hints-- + +Your `Stack` class should have a `collection` property initialized to an empty array. + +```js +const test = new Stack(); +assert.isArray(test.collection); +assert.lengthOf(test.collection, 0); +``` + +Your `Stack` class should have a `push` method. + +```js +const test = new Stack(); +assert.isFunction(test.push); +``` + +The `push` method should add an element to the end of the `collection` array. + +```js +const test = new Stack(); +test.push('CS61'); +assert.lengthOf(test.collection, 1); +assert.strictEqual(test.collection[0], 'CS61'); +test.push('CS50'); +assert.lengthOf(test.collection, 2); +assert.strictEqual(test.collection[0], 'CS61'); +assert.strictEqual(test.collection[1], 'CS50'); +``` + +Your `Stack` class should have a `pop` method. + +```js +const test = new Stack(); +assert.isFunction(test.pop); +``` + +Your `Stack` class should have a `peek` method. + +```js +const test = new Stack(); +assert.isFunction(test.peek); +``` + +Your `Stack` class should have an `isEmpty` method. + +```js +const test = new Stack(); +assert.isFunction(test.isEmpty); +``` + +Your `Stack` class should have a `clear` method. + +```js +const test = new Stack(); +assert.isFunction(test.clear); +``` + +The `peek` method should return the top element of the stack. + +```js +const test = new Stack(); +test.push('CS61'); +test.push('CS50'); +assert.strictEqual(test.peek(), 'CS50'); +assert.strictEqual(test.peek(), 'CS50'); +``` + +The `pop` method should remove and return the top element of the stack. + +```js +const test = new Stack(); +test.push('CS61'); +test.push('CS50'); +assert.strictEqual(test.pop(), 'CS50'); +assert.lengthOf(test.collection, 1); +assert.strictEqual(test.collection[0], 'CS61'); +assert.strictEqual(test.pop(), 'CS61'); +assert.lengthOf(test.collection, 0); +``` + +The `isEmpty` method should return `true` if the stack does not contain any elements. + +```js +const test = new Stack(); +assert.isTrue(test.isEmpty()); +``` + +The `isEmpty` method should return `false` if the stack contains elements. + +```js +const test = new Stack(); +test.push('CS61'); +assert.isFalse(test.isEmpty()); +``` + +The `clear` method should remove all element from the stack + +```js +const test = new Stack(); +test.push('CS61'); +test.push('CS50'); +test.clear(); +assert.isTrue(test.isEmpty()); +``` + +# --seed-- + +## --seed-contents-- + +```js + +``` + +# --solutions-- + +```js +class Stack { + constructor() { + this.collection = []; + } + + push = function (item) { + this.collection.push(item); + } + + pop = function () { + return this.collection.pop(); + } + + peek = function () { + return this.collection[this.collection.length - 1]; + } + + isEmpty = function () { + return this.collection.length === 0; + } + + clear = function () { + this.collection.length = 0; + } +} +``` diff --git a/curriculum/test/utils/mongo-ids.js b/curriculum/test/utils/mongo-ids.js index 636fa647fe0..4cf405f211c 100644 --- a/curriculum/test/utils/mongo-ids.js +++ b/curriculum/test/utils/mongo-ids.js @@ -85,6 +85,7 @@ const duplicatedProjectIds = [ // Shopping Cart // Stacks + '587d8250367417b2b2512c5f', // Linked Lists