|
| 1 | +/** |
| 2 | + * @description Initializes stack as an array limited by max size |
| 3 | + * @param {Number} maxSize |
| 4 | + */ |
| 5 | +const CustomStack = function (maxSize) { |
| 6 | + this.stack = []; |
| 7 | + this.maxSize = maxSize; |
| 8 | +}; |
| 9 | + |
| 10 | +/** |
| 11 | + * @description Adds value to the top of the stack |
| 12 | + * @param {Number} value |
| 13 | + * @return {Void} |
| 14 | + */ |
| 15 | +CustomStack.prototype.push = function (value) { |
| 16 | + if (this.stack.length < this.maxSize) { |
| 17 | + this.stack.push(value); |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * @description Returns the top of the stack and removes it |
| 23 | + * @return {Number} |
| 24 | + */ |
| 25 | +CustomStack.prototype.pop = function () { |
| 26 | + // Забираем элемент из стека |
| 27 | + if (this.stack.length === 0) { |
| 28 | + return -1; |
| 29 | + } |
| 30 | + const takenElement = this.stack.pop(); |
| 31 | + return takenElement; |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * @description Increments the bottom number of elements by value |
| 36 | + * @param {Number} number |
| 37 | + * @param {Number} value |
| 38 | + * @return {Void} |
| 39 | + */ |
| 40 | +CustomStack.prototype.increment = function (number, value) { |
| 41 | + // Сначала сравниваем размер стека с k |
| 42 | + if (this.stack.length < number) { |
| 43 | + this.stack = this.stack.map((elem) => elem + value); |
| 44 | + } else { |
| 45 | + for (let i = 0; i < number; i += 1) { |
| 46 | + const elem = this.stack[i]; |
| 47 | + this.stack[i] = elem + value; |
| 48 | + } |
| 49 | + } |
| 50 | +}; |
0 commit comments