JSFiddle - React, Tailwind, and code Playground
by Erik East
HTML
<h2>Module 04</h2>
<h3>Assignment 1</h3>
<p>Input a number: <input type='text' style="width: 50px;" id='theoutput' onkeypress='captureEnter(event);'></p>
<p id="demo"></p>
JavaScript
var theStack = [];
var input = '';
function captureEnter(e){
if(e.keyCode === 13){
stackComputer();
}
return false;
}
function stackComputer(){
var theinput = document.getElementById('theoutput').value;
if(!isNaN(parseInt(theinput, 10))){
theStack.push(theinput);
}//if
else if(theinput=='+'){
var num1 = theStack.pop();
var num2 = theStack.pop();
var add = parseInt(num1) + parseInt(num2);
theStack.push(add);
}//else if addition
else if(theinput=='-'){
var num3 = theStack.pop();
var num4 = theStack.pop();
var subtract = num3 - num4;
theStack.push(subtract);
}//else if subtraction
else if(theinput=='*'){
var num5 = theStack.pop();
var num6 = theStack.pop();
var multiply = num5 * num6;
theStack.push(multiply);
}//else if mutliplication
else if(theinput=='/'){
var num7 = theStack.pop();
var num8 = theStack.pop();
var divide = num7 / num8;
theStack.push(divide);
}//else if division
document.getElementById('theoutput').value = '';
displayStack();
}//function stackComputer
function displayStack (){
document.getElementById('demo').innerHTML = '' ;
for(var i = 0; i < theStack.length; i++){
document.getElementById('demo').innerHTML += theStack[i] + ' | ';
}//for loop output
}//function printStack