Copyright

Implement a Stack computer in Javascript. This is a simple computer that keeps a stack, when a...

Question:

Implement a Stack computer in Javascript. 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.

Implementation of a Stack computer in Javascript:

A stack is an Abstract Data Type (ADT), used in many programming languages.

LIFO stands for Last-in-first-out.

The element which is placed (inserted or added) last, is fetched first. In stack, insertion operation is called PUSH operation and removal operation is called POP operation.

Answer and Explanation:

Become a Study.com member to unlock this answer! Create your account

View this answer

Following is the Javascript code :

function Stack() {
 
   var items = [];
 
   this.push = function(element){
&...

See full answer below.


Learn more about this topic:

Loading...
What is JavaScript? - Function & Uses

from

Chapter 11 / Lesson 21
11K

In order to build today's websites, you'll need to know more than just HTML. This lesson introduces you to JavaScript, which allows you to put code into your websites to increase their functionality.


Related to this Question

Explore our homework questions and answers library