Implement a Stack computer in Javascript (you will turn in a link to your progra
ID: 665084 • Letter: I
Question
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
working code on fiddle
var stack = [];
var first=prompt("enter the first number");
stack.push(first);
var second=prompt("enter the second number");
stack.push(second);
var op=prompt("enter the operation");
first = stack.pop();
second =stack.pop();
if (op == "*")
var final = first * second;
else if (op == "+")
var final = +first + +second;
else
var final = +first - +second;
stack.push(final);
var i = stack.pop();
alert(i);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.