Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
SetofStacks
class Stack { constructor() { this.values = []; } push(value) { this.values.push(value) } pop() { return this.values.pop(); } peek() { return this.values[this.values.length - 1]; } size() { return this.values.length; } isEmpty() { return this.values.length === 0; } } class SetOfStacks{ constructor(maxSize) { this.maxSize = maxSize; this.stacks = [new Stack()]; } push(value) { if (this.stacks[this.stacks.length - 1].size() >= this.maxSize) { let stack = new Stack(); stack.push(value); this.stacks.push(stack); } else { this.stacks[this.stacks.length - 1].push(value); } } pop() { let popValue = this.stacks[this.stacks.length - 1].pop() // remove stack if nothing in it if (this.stacks.length !== 1 && this.stacks[this.stacks.length - 1].size() === 0) { this.stacks.pop(); } return popValue; } popAt(num) { let popValue = this.stacks[num].pop() // remove stack if nothing in it if (this.stacks.length !== 1 && this.stacks[num].size() === 0) { this.stacks.pop(); } return popValue; } peek() { return this.stacks[this.stacks.length - 1].peek(); } isEmpty() { return this.stacks[0].size() === 0; } } let stack = new SetOfStacks(3); console.log(stack.isEmpty()); stack.push(1) stack.push(2) stack.push(3) stack.push(4) stack.push(5) console.log(stack); stack.push(5) stack.push(5) stack.push(1) console.log(stack.popAt(0)); console.log(stack.popAt(1)); console.log(stack.popAt(2));
run
|
edit
|
history
|
help
0
JS exam part 2 - Circumference
test
WC
js object to json string
Oh my numbers!
H
JavaScript - CheckDate()
rstring
123141242342
Triangle