Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why can I not enter operand into the input box so that the last two numbers in the stack get added, subtracted, divided, or multiplied

Why can I not enter operand into the input box so that the last two numbers in the stack get added, subtracted, divided, or multiplied by their respective operands (+,-,/,*) Also combine the push to stack function and evaluate function into one function. I will thumbs up if the code works!
RPN Calculator
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){
this.displayOutput("Stack is empty");
return null;
}
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;
}
this.displayOutput("Contents of Stack: "+ stackContents.join(""));
}
displayOutput(message){
document.getElementById("stackOutput").innerText = message;
}
}
const stack = new Stack();
function pushToStack(){
const input = document.getElementById("stackInput").value;
if (!isNaN(input)){
stack.push(parseFloat(input));
} else {
stack.displayOutput("Invalid input: Please enter a number");
}
document.getElementById("stackInput").value =''; // Clear the input box after push
}
function evaluate(){
const input = document.getElementById("stackInput").value;
let result;
if (["+","-","*","/"].includes(input)){
const operand2= stack.pop();
const operand1= stack.pop();
if (operand1!== null && operand2!== null){
switch(input){
case '+':
result = operand1+ operand2;
break;
case '-':
result = operand1- operand2;
break;
case '*':
result = operand1* operand2;
break;
case '/':
if (operand2===0){
stack.displayOutput("Error: Division by zero");
return;
}
result = operand1/ operand2;
break;
}
stack.push(result);
} else {
stack.displayOutput("Error: Not enough operands");
}
} else {
stack.displayOutput("Invalid operator: Please enter +,-,*, or /");
}
document.getElementById("stackInput").value =''; // Clear the input box after evaluation
}

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions