Answered step by step
Verified Expert Solution
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 digit, so whenever I click more than digit, it doesnt save the first digit.
let buffer ;
let runningTotal ;
let previousOperator null;
const screen document.querySelectorscreen';
function buttonClickvalue
console.logvalue;
NaN is not a number
if isNaNparseIntvalue
handleSymbolvalue;
else
handleNumbervalue;
rerender;
function handleNumbernumber
if buffer
buffer number;
else
buffer buffer number;
function handleMathvalue
ifbuffer
do nothing
return;
const intBuffer parseIntbuffer;
if runningTotal
runningTotal intBuffer;
else
flushOperationintBuffer;
previousOperator value;
buffer ;
function flushOperationintBuffer
if previousOperator
runningTotal runningTotal intBuffer;
else if previousOperator
runningTotal runningTotal intBuffer;
else if previousOperator x
runningTotal runningTotal intBuffer;
else
runningTotal runningTotal intBuffer;
function handleSymbolsymbol
if symbol AC
switch symbol
case AC:
buffer ;
break;
case :
ifpreviousOperator null
return;
flushOperationparseIntbuffer;
previousOperator null;
buffer runningTotal;
runningTotal ;
break;
case 'Del':
ifbufferlength
buffer ;
else
buffer buffer.substring buffer.length ;
break;
case :
case :
case :
case x:
handleMathsymbol;
break;
function init
document.querySelectorcalcbuttons'
addEventListenerclick functionevent
buttonClickeventtarget.innerText;
function rerender
screen.innerText buffer;
document.getElementsByClassNamecalcbutton";
const buttons document.getElementsByClassNamecalcbutton";
buttonList Array.prototype.slice.callbuttons;
buttonList.forEachbutton button.addEventListenerclickevent buttonClickeventtarget.textContent;
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