feat(curriculum): add stack class lab (#56014)
parent
c253d32147
commit
343c244a84
|
@ -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": [
|
||||
|
|
|
@ -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.
|
|
@ -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"
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
```
|
|
@ -85,6 +85,7 @@ const duplicatedProjectIds = [
|
|||
// Shopping Cart
|
||||
|
||||
// Stacks
|
||||
'587d8250367417b2b2512c5f',
|
||||
|
||||
// Linked Lists
|
||||
|
||||
|
|
Loading…
Reference in New Issue