Published
Edited
May 15, 2022
Insert cell
Insert cell
class Stack extends Array {
// Constructor
constructor (size = 10, ...values) {
super(...values)
this.size = size;
}
// True if stack is empty
isEmpty() {
return this.length == 0;
}

// True if stack is full
isFull() {
return this.length == this.size;
}
// Adds x to the top of the stack
add(item) {
if (this.isFull()) {
throw 'Stack Overflow'
} else {
this.push(item);
}
}

// Removes an element from the top of the stack
remove() {
if (this.isEmpty()) {
throw 'Stack Underflow';
} else {
this.pop();
}
}
}
Insert cell
Insert cell
Insert cell
myStack = new Stack();
Insert cell
Insert cell
{
let myStack = new Stack();
myStack.add(0);
myStack.add(1);
myStack.add(2);
return myStack;
}
Insert cell
Insert cell
{
let myStack = new Stack();
myStack.add(0);
myStack.add(1);
myStack.add(2);
myStack.remove();
return myStack;
}
Insert cell
Insert cell
{
let myStack = new Stack();
myStack.add(0);
myStack.remove();
myStack.remove();
return myStack;
}
Insert cell
Insert cell
{
let myStack = new Stack(2);
myStack.add(0);
myStack.add(1);
myStack.add(2);
return myStack;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more