Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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);       

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote