Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!