Question: The question keeps getting answered wrong please read the instructions before answering. This is my fifth time asking,it does not seem right that I have
The question keeps getting answered wrong please read the instructions before answering. This is my fifth time asking,it does not seem right that I have to waste all of my questions because it keeps getting answered wrong.
I need a working jsfiddle. The html and javascript must be separate
.
Assignment
Implement a Stack in Javascript (you will turn in a link to your program in JSFiddle). Do not use an array as the stack or in the implementation of the stack. Repeat - You MUST implement the Stack (start with your linked list) without the use of an array..
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.
The program should use a simple input box, either a text field or prompt and display the contents of the Stack.
Contents of Stack:
Information
Building A Stack is relatively easy if you have built a List. A Stack is nothing more complex with 2 extra functions added Push(_value) and Pop(). The Push function obviously needs an argument _value. The Pop function should always return the element on the top of the stack and at the same time removing it from the top of the stack.
Here is what I have but the calculations do not seem to work correctly.4+5 should not = 54
L1 = [];
Q1 = [];
var all_count = 1;
var setter = 0;
var Index = 0;
var count = 1;
var html = " ";
function List_one(){
var limit = prompt("please enter a number you would like to fill L1 to.");
for(i = 2; i <= limit; i++)
{
L1[setter] = i;
setter++;
}
while(all_count <= L1.length)
{
var value = L1[0];
Q1[Index] = value;
Index++;
L1.shift();
for(i = 0; i <= L1.length; i++){
if(L1[i] % value == 0)
{
L1.splice(i, 1);
}
}
html += "
document.getElementById("demo").innerHTML = html;
}
}
function Stack() {
var items = [];
this.push = function(element){
items.push(element);
};
this.pop = function(){
return items.pop();
};
this.peek = function(){
return items[items.length-1];
};
this.isEmpty = function(){
return items.length == 0;
};
this.size = function(){
return items.length;
};
this.clear = function(){
items = [];
};
this.print = function(){
alert("Stack Elements are:"+items.toString());
};
}
//Decalre stack object
var stack = new Stack();
while(1)
{
var element = prompt("Enter stack element,(q or Q to exit)", "");
if(element == 'q' || element =='Q')
{
break;
}
else
{
if(element=='*'
|| element=='+' || element=='-' || element=='/')
{
//Check if stack is empty
if(stack.isEmpty() || stack.size<2 )
{
alert("Invalid Operation, Stack is empty or size is less than 2");
}
else
{
var op1 = stack.pop();
var op2 = stack.pop();
if(element=='*')
{
var res = op1 * op2;
}
else if(element=='+')
{
var res = op1 + op2;
}
else if(element=='-')
{
var res = op1 - op2;
}
else if(element=='/')
{
var res = op1 / op2;
}
stack.push(res);
stack.print();
}
}
else
{
stack.push(element);
stack.print();
}
}
}
alert("Thank You!");
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
