JSFiddle - React, Tailwind, and code Playground
by HSilvaDaytona
HTML
<input type="textbox" id='value'/>
<input type="button" id="push" value="Push to stack" onclick="push()"/>
<p id="output"></p>
JavaScript
document.getElementById("push").onclick = function()
{
push();
};
var Node = function(itemsAdd)
{
this.next = null;
this.last=null;
this.content = itemsAdd;
}
var Stack = function(){
this.top = null;
this.bottom = null;
this.push = function(itemsAdd){
if(this.bottom== null){
this.bottom = new Node(itemsAdd);
this.top = this.bottom;
return this;
}
var addedNode = new Node(itemsAdd);
addedNode.last = this.top;
this.top.next = addedNode;
this.top = addedNode;
}
this.pop = function(){
if(this.bottom == null){
document.getElementById('output').innerHTML = "empty";
return null;
}
if(this.bottom == this.top){
this.top = this.bottom ;
this.top.last= null;
return this.top;
}
var a = this.top; //this holds the value at the top
this.top = this.top.last;
this.top.next = null;
return a;//top of stack
}
this.toString = function() {
var str = "";
var node = this.bottom;
while (node != null) {
str += node.content + " ";
node = node.next; //moves on to the next node
}
return str;
}
}//end stack
function clearDisplay(){ //clears everytime the button is pushed
var d="";
document.getElementById("value").value="";
}
var stack = new Stack();
var a;
var b;
var c;
var result;
var input;
var answer;
function push(){
input=document.getElementById("value").value;
stack.push(input);
document.getElementById('output').innerHTML = stack.toString();
clearDisplay();
if (input === "+") {
clearDisplay();
varUse();
answer = a1 + b1;
stack.push(answer);
document.getElementById('output').innerHTML=stack.toString();
} else if (input === "-") {
varUse();
answer = a1 - b1;
stack.push(parseInt(b) - parseInt(a));
document.getElementById('output').innerHTML= stack.toString();
} else if (input === "*") {
varUse();
answer = a1 * b1;
stack.push(parseInt(a) * parseInt(b));
document.getElementById('output').innerHTML= stack.toString();
} else if (input === "/")...