mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
Update step2-04 exercises
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
describe('stack', () => {
|
||||
// Import the stack here
|
||||
|
||||
describe('Stack', () => {
|
||||
it('should push item to the top of the stack', () => {
|
||||
// implement test here:
|
||||
// 1. require the stack
|
||||
// 2. create stack push calls to place some items in the stack
|
||||
// 3. write assertions via the expect() API
|
||||
// 1. Create a stack instance
|
||||
// 2. Use stack push calls to add some items to the stack
|
||||
// 3. Write assertions via the expect() API
|
||||
});
|
||||
|
||||
it('should pop the item from the top of stack', () => {
|
||||
// implement test here:
|
||||
// 1. require the stack
|
||||
// 2. create stack push calls to place some items in the stack
|
||||
// 1. Create a stack instance
|
||||
// 2. Use stack push calls to add some items to the stack
|
||||
// 3. pop a few items off the stack
|
||||
// 4. write assertions via the expect() API
|
||||
});
|
||||
|
||||
@@ -1,13 +1,27 @@
|
||||
export class Stack<T> {
|
||||
private _items: T[] = [];
|
||||
|
||||
/** Add an item to the top of the stack. */
|
||||
push(item: T) {
|
||||
this._items.push(item);
|
||||
}
|
||||
|
||||
/** Remove the top item from the stack and return it. */
|
||||
pop(): T {
|
||||
if (this._items.length > 0) {
|
||||
return this._items.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/** Return the top item from the stack without removing it. */
|
||||
peek(): T {
|
||||
if (this._items.length > 0) {
|
||||
return this._items[this._items.length - 1];
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the number of items in the stack/ */
|
||||
get count(): number {
|
||||
return this._items.length;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user