Answered step by step
Verified Expert Solution
Link Copied!

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 0 while I'm inputting integers how do I fix? // Node class
class Node {
constructor(data){
this.data = data;
this.next = null;
}
}
// Stack class
class Stack {
constructor(){
this.top = null;
this.size =0;
}
// Push operation
push(data){
// Create a new node with the data
let node = new Node(data);
// If the stack is empty, set the node as the top node
if (this.isEmpty()){
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 (this.isEmpty()){
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.push(current.data);
// 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 =0;
}
// Count operation
count(){
// Return the size of the stack
return this.size;
}
// Peek operation
peek(){
// If the stack is empty, return null
if (this.isEmpty()){
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 ===0;
}
}
let stack = new Stack();
// Calculator function
function btnClick(){
// Function to read input data and perform operation
// Getting input data
let input = document.getElementById("input").value;
let buttons = document.querySelectorAll("button");
for (let button of buttons){
button.addEventListener("click", btnClick);
}
// Checks for number
if (isNaN(input)== false){
// Converting to number from string
input = Number(input);
// Pushing to stack
stack.push(input);
document.getElementById("msg").innerHTML = input +" Added to stack";
console.log(input +" Added to stack");
} else {
// Check for operator
if (stack.count()>1){
// 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 = doMath(x, y, input);
// Pushing result to stack
stack.push(result);
document.getElementById("msg").innerHTML = "Result: "+ result;
console.log("valid operator");
console.log("Result:"+ result);
} else {
// Message for invalid operator
console.log("Invalid operator");
document.getElementById("msg").innerHTML = "Invalid operator(try: +,-,*,/,%)";
}
} else {
// Message for few operands
document.getElementById("msg").innerHTML = "Not much value to perform task";
console.log("Not much value to perform task");
}
}
document.getElementById("input").value ="";
}
// Function to perform operation
function doMath(x, 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

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

A Complete Guide To Data Science Essentials

Authors: Miguel

1st Edition

9358684992, 978-9358684995

More Books

Students also viewed these Databases questions

Question

=+3. What resources will these tactics require?

Answered: 1 week ago