JSFiddle - React, Tailwind, and code Playground

by ronilan

JavaScript

// this is an implementation of a stack
function Stack() {
  this.store = [];
}

Stack.prototype.push = function(item){
    this.store.push(item);
}

Stack.prototype.pop = function(){
    return this.store.pop();
}

// should return item or null;
Stack.prototype.find = function(item){
    
    var i,
        max = this.store.length,
        result = null;
        
    for (i = 0; i < max; i++){
        // this is naive
        if (this.store[i] === item) {
            result = this.store[i];
            break;
        }
        
    }
    
    return result;
    
}


stack = new Stack();

stack.push("1 hi");
stack.push("2 there");
stack.push("3 might work");

// should give us "3 might work"
console.log(stack.pop());

// should give us "1 hi"
console.log(stack.find("1 hi"));


// should give us null
console.log(stack.find("dlfgjldfgklda"));