Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using JSFiddle Im trying to do a RPN Stack You will build a Stack Computer from your stack. When a number is entered it goes

Using JSFiddle Im trying to do a RPN Stack

You will build a Stack Computer from your 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 and the result is pushed onto the top of the stack. This is how an RPN calculator.

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

So looking above, I got to stack but I need to look like above like when the 2nd digit is enter mine looks like 2 5 not 5 2. The Stack has to be on top or first and I need addition, multiplication, subtraction, and division added.

.HTML:

Javascript

var Stack = function() { // create array for stack this.stack = []; this.push = function(number){ // push number to array means stack this.stack.push(number); } this.pop = function(number){ // pop last element from array return this.stack.pop(); } this.getString = function(){ // get all elements in array with space between return this.stack.join(' '); } };

function clearDisplay() { // clear display document.getElementById("value").value = ""; }; var stack = new Stack(); var a; var b; var c; var result; var input;

function push() //added push function { input = document.getElementById("value").value;// gets the value input = input.replace(/ /g,'');// replace space with empty string if (!input) { // if input not present alert user alert("Please enter a number"); }else{ if(parseInt(input)){ // check input is number stack.push(parseInt(input)); // if number push number to stack clearDisplay(); // clear input }else{ // if input not a number check for operations switch(input){ case '+': // if plus pop last two contents of stack and add it and push to stack a = stack.pop(); b = stack.pop(); stack.push(a+b); break; case '-': // if minus pop last two contents of stack and subtract it and push to stack a = stack.pop(); b = stack.pop(); stack.push(b-a); break; case '*': // if multiplication pop last two contents of stack and multiply it and push to stack a = stack.pop(); b = stack.pop(); stack.push(a*b); break; case '/': // if division pop last two contents of stack and divide it and push to stack a = stack.pop(); b = stack.pop(); stack.push(b/a); break; default: // if otherthan above operations alert user wrong input alert('Wrong input'); } } } document.getElementById('output').innerHTML = stack.getString(); // display output clearDisplay(); };

Please add the sample output from jsfiddle

remember number needs to stack at the top like example above

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Apache Cassandra 3 X An Expert Guide To Improving Database Scalability And Availability Without Compromising Performance

Authors: Aaron Ploetz ,Tejaswi Malepati ,Nishant Neeraj

3rd Edition

1789131499, 978-1789131499

More Books

Students also viewed these Databases questions