Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help to fix this js . I created the calculator, but the js file doesnt complete. The input only take 1 digit, so

I need help to fix this js. I created the calculator, but the js file doesnt complete. The input only take 1 digit, so whenever I click more than 1 digit, it doesnt save the first digit.
let buffer ="0";
let runningTotal =0;
let previousOperator =null;
const screen = document.querySelector('.screen');
function buttonClick(value){
console.log(value);
//NaN is not a number
if (isNaN(parseInt(value))){
handleSymbol(value);
} else {
handleNumber(value);
}
rerender();
}
function handleNumber(number){
if (buffer ==="0"){
buffer = number;
} else {
buffer == buffer + number;
}
}
function handleMath(value){
if(buffer ==="0"){
//do nothing
return;
}
const intBuffer = parseInt(buffer);
if (runningTotal ===0){
runningTotal = intBuffer;
} else {
flushOperation(intBuffer);
}
previousOperator = value;
buffer ="0";
}
function flushOperation(intBuffer){
if (previousOperator ==='+'){
runningTotal = runningTotal + intBuffer;
} else if (previousOperator ==='-'){
runningTotal = runningTotal - intBuffer;
} else if (previousOperator ==='x'){
runningTotal = runningTotal * intBuffer;
} else {
runningTotal = runningTotal / intBuffer;
}
}
function handleSymbol(symbol){
//if (symbol ==='AC')
switch (symbol){
case 'AC':
buffer ='0';
break;
case '=':
if(previousOperator === null){
return;
}
flushOperation(parseInt(buffer));
previousOperator = null;
buffer =""+ runningTotal;
runningTotal =0;
break;
case 'Del':
if(buffer.length ===1){
buffer ='0';
} else {
buffer = buffer.substring(0, buffer.length -1);
}
break;
case '+':
case '-':
case '/':
case 'x':
handleMath(symbol);
break;
}
}
function init(){
document.querySelector('.calc-buttons')
.addEventListener("click", function(event){
buttonClick(event.target.innerText);
})
}
function rerender(){
screen.innerText = buffer;
}
document.getElementsByClassName("calc-button");
const buttons = document.getElementsByClassName("calc-button");
buttonList = Array.prototype.slice.call(buttons);
buttonList.forEach((button)=> button.addEventListener("click",(event)=> buttonClick(event.target.textContent)));-------

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

may you please give me the references for this answers

Answered: 1 week ago

Question

Conduct an effective performance feedback session. page 360

Answered: 1 week ago