Please Separate HTML and JAVA coding and show proof if possible! Implement a Sta
ID: 668315 • Letter: P
Question
Please Separate HTML and JAVA coding and show proof if possible!
Implement a Stack computer in Javascript (you will turn in a link to your program in JSFiddle). This is a simple computer that keeps a stack, when a number is entered it goes onto the top of the stack. When an operation is entered, the previous 2 numbers are operated on by the operation.
For example
2 [enter] 2
5 [enter] 5 2
* [enter] * 5 2 -> collapses to 10
would leave at 10 at the top of the stack.
The program should use a simple input box, either a text field or prompt and display the contents of the Stack.
Explanation / Answer
<html>
<head>
var stack = [] ;
function store()
{
var input = document.getelementbyId("box").value;
if(input=="*" || "+" || "-"||"/")
{
calculate(input);
}
else
{
stack.splice(0,0,input);
}
}
function calculate(sign)
{var result;
if(sign == "+")
{
result = stack[0]+stack[1];
}
else if(sign == "-")
{
result = stack[0]-stack[1];
}
else if(sign == "*")
{
result = stack[0]*stack[1];
}
else if(sign == "/")
{
result = stack[0]/stack[1];
}
stack.splice(1,0);
stack.splice(1,0);
stck.splice(0,0,result);
alert(result);
}
</head>
<body>
enter number
<input id="box" type="text">
</body>
</html>