Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would I write the HTML for this JS stack code to show an input box on a webpage that you can enter numbers or

How would I write the HTML for this JS stack code to show an input box on a webpage that you can enter numbers or operands into and that has a button next to it thats says push to stack. Also the html should display the computers output on the webpage not console. You can augment the JS code as need be. I will thumbs up if it works! class Node {
constructor(data){
this.data = data;
this.next = null;
}
}
class Stack {
constructor(){
this.top = null;
}
push(data){
const newNode = new Node(data);
newNode.next = this.top;
this.top = newNode;
this.display();
}
pop(){
if (!this.top){
console.log("Stack is empty");
return;
}
const poppedData = this.top.data;
this.top = this.top.next;
this.display();
return poppedData;
}
display(){
let current = this.top;
const stackContents =[];
while (current){
stackContents.push(current.data);
current = current.next;
}
console.log("Contents of Stack:", stackContents.join(''));
}
}
function rpnCalculator(){
const stack = new Stack();
while (true){
const entry = prompt("Enter a number or operator (q to quit):");
if (entry ==='q'){
break;
} else if (!isNaN(entry)){
stack.push(parseFloat(entry));
} else if (entry ==='+'|| entry ==='-'|| entry ==='*'|| entry ==='/'){
const operand2= stack.pop();
const operand1= stack.pop();
switch (entry){
case '+':
stack.push(operand1+ operand2);
break;
case '-':
stack.push(operand1- operand2);
break;
case '*':
stack.push(operand1* operand2);
break;
case '/':
stack.push(operand1/ operand2);
break;
default:
console.log("Invalid operator");
}
} else {
console.log("Invalid input");
}
}
}
rpnCalculator();

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

Step: 3

blur-text-image

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

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

Explain the importance of effective communication

Answered: 1 week ago

Question

* What is the importance of soil testing in civil engineering?

Answered: 1 week ago

Question

Explain the concept of shear force and bending moment in beams.

Answered: 1 week ago

Question

=+2 Is the decision sustainable in the long run?

Answered: 1 week ago

Question

=+1 Is the decision fair to employees?

Answered: 1 week ago