Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This piece of js code is supposed to implement a stack using a list ( NOT AN ARRAY ) . It is supposed to only
This piece of js code is supposed to implement a stack using a list NOT AN ARRAY It is supposed to only allow integers and operands. When an integer is entered it pushes it to the stack. if an operand is entered it does an equation using the previous two inputed integers. My code keeps randomly returning a while Im inputting integers how do I fix? Node class
class Node
constructordata
this.data data;
this.next null;
Stack class
class Stack
constructor
this.top null;
this.size ;
Push operation
pushdata
Create a new node with the data
let node new Nodedata;
If the stack is empty, set the node as the top node
if thisisEmpty
this.top node;
else
If the stack is not empty, set the node as the next of the top node
node.next this.top;
Set the node as the new top node
this.top node;
Increment the size of the stack
this.size;
Pop operation
pop
If the stack is empty, return null
if thisisEmpty
return null;
else
If the stack is not empty, get the data of the top node
let data this.top.data;
Set the next node as the new top node
this.top this.top.next;
Decrement the size of the stack
this.size;
Return the data
return data;
Show operation
show
Initialize an empty array to store the data
let items ;
Initialize a current node as the top node
let current this.top;
Loop through the stack until the current node is null
while current
Push the data of the current node to the array
items.pushcurrentdata;
Set the current node as the next node
current current.next;
Return the array
return items;
Clear operation
clear
Set the top node as null
this.top null;
Set the size as zero
this.size ;
Count operation
count
Return the size of the stack
return this.size;
Peek operation
peek
If the stack is empty, return null
if thisisEmpty
return null;
else
If the stack is not empty, return the data of the top node
return this.top.data;
IsEmpty operation
isEmpty
Return true if the size is zero, false otherwise
return this.size ;
let stack new Stack;
Calculator function
function btnClick
Function to read input data and perform operation
Getting input data
let input document.getElementByIdinputvalue;
let buttons document.querySelectorAllbutton;
for let button of buttons
button.addEventListenerclick btnClick;
Checks for number
if isNaNinput false
Converting to number from string
input Numberinput;
Pushing to stack
stack.pushinput;
document.getElementByIdmsginnerHTML input Added to stack";
console.loginput Added to stack";
else
Check for operator
if stackcount
If stack has more than one elements
if input input input input input
If valid operator given
Popping operands
let y stack.pop;
let x stack.pop;
Calculating result
let result doMathx y input;
Pushing result to stack
stack.pushresult;
document.getElementByIdmsginnerHTML "Result: result;
console.logvalid operator";
console.logResult: result;
else
Message for invalid operator
console.logInvalid operator";
document.getElementByIdmsginnerHTML "Invalid operatortry: ;
else
Message for few operands
document.getElementByIdmsginnerHTML "Not much value to perform task";
console.logNot much value to perform task";
document.getElementByIdinputvalue ;
Function to perform operation
function doMathx y o
switch o
case :
return x y;
case :
return x y;
case :
return x y;
case :
return x y;
case :
return x y;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started