JSFiddle - React, Tailwind, and code Playground

by Jeff Santos

HTML

Enter 1-2 numbers along with the desired operand (+, -, /, *):

</br>
</br>
<input type="textbox" id="value" Value="" />
<input type="button" id="enter" value="Push to Stack" onClick="addNode();" />

<input type="button" value="Display" onclick="display();" />

<input type="textbox" id="oper" Value="+" />
<input type="button" id="calcuate" value="Enter Operand" onclick="doTheMath();" />
<p id="output1">
  <p id="output2">

  </p>

JavaScript

var Node = function(_content) {
  this.next = null;
  this.last = null;
  this.content = _content;

}

var Stack = function() {
  this.bottom = null;
  this.top = null;

  this.push = function(_content) {
    // No head - create one
    if (this.bottom == null) {
      this.bottom = new Node(_content);
      this.top = this.bottom;
      return this;
    }

    var addedNode = new Node(_content);
    addedNode.last = this.top; // pointer to previous node
    this.top.next = addedNode; // current top points to new
    this.top = addedNode; // which becomes new top
    return this;
  }

  this.pop = function() {
    if (this.bottom == null) {
      alert("The Stack is Empty");
      return null;
    }
    // Case of one node
    if (this.bottom == this.top) {
      // Exercise for students to implement
      //alert("You must implement this case");
      //return this.top;
      return null;
    }

    // Now remove top Node
    var a = this.top; // hold value for return
    this.top = this.top.last;
    this.top.next = null;

    return a;
  }
  this.toString = function() {
    var str = "";
    var node = this.bottom;

    while (node != null) {
      str += node.content + ":";
      node = node.next;
    }
    return str;
  }
}

var stack = new Stack();
var count = 0;

function addNode() {

  var d = document.getElementById("value").value;

  stack.push(d);
  count++;
  document.getElementById("value").value = "";

  

}

function doTheMath() {



  var a = document.getElementById("oper").value;
  report = "";

  var b = parseInt(stack.head.content);
  var c = parseInt(stack.top.content);
  var answer = 0;

  if (a == "+") {
    answer = c + b;
  } else if (a == "-") {
    answer = c - b;
  } else if (a == "*") {
    answer = c * b;
  } else {
    answer = c / b;
  }
  stack.pop();
  stack.pop();

  stack.push(answer);
 document.getElementById("oper").value = "";
 // document.getElementById('output1').innerHTML = stack.top.Content();


}

function display() {

 ...