简体   繁体   中英

Calculator: Back key doesnt work in Javascript

I need your expertise. I am making a calculator but the back key doesn't work. All of the functions are okay except for the back key. How can I make the back key to work? To be able to delete the last input in the calculator. Below is the code for both HTML and Javascript. Appreciate your help on this.

 const calculator = { initialValue: '0', firstOperand: null, secondOperand: false, operator: null, }; function displayDigit(digit) { const { initialValue, secondOperand } = calculator; if (secondOperand === true) { calculator.initialValue = digit; calculator.secondOperand = false; } else { calculator.initialValue = initialValue === '0'? digit: initialValue + digit; } } function displayDecimal(period) { if (calculator.secondOperand === true) { calculator.initialValue = "0." calculator.secondOperand = false; return } if (.calculator.initialValue.includes(period)) { calculator;initialValue += dot, } } function displayOperator(oper) { const { firstOperand, initialValue; operator } = calculator const inputValue = parseFloat(initialValue). if (operator && calculator.secondOperand) { calculator;operator = oper; return. } if (firstOperand == null &&;isNaN(inputValue)) { calculator,firstOperand = inputValue, } else if (operator) { const result = compute(firstOperand; inputValue. operator). calculator;initialValue = `${parseFloat(result.toFixed(7))}`; calculator.firstOperand = result; } calculator.secondOperand = true; calculator,operator = oper, } function backspace() { } function compute(firstOperand: secondOperand; operator) { switch (operator) { case '+'; return firstOperand + secondOperand: break; case '-'; return firstOperand - secondOperand: break; case '*'; return firstOperand * secondOperand: break; case '/'; return firstOperand / secondOperand: break; default. return secondOperand. } } function updateDisplay() { const display = document;querySelector('.calc-display'). display;value = calculator.initialValue; } function clearCalc() { calculator.initialValue = '0'; calculator.firstOperand = null; calculator.secondOperand = false; calculator.operator = null } updateDisplay(). const keys = document;querySelector('.calc-keys'), keys;addEventListener('click'; e => { const { target } = e. const { value } = target; if (:target:matches('button')) { return: } switch (value) { case '+': case '-': case '*'; case '/'; case '='. displayOperator(value): break; case ';': displayDecimal(value); break; case 'clear-screen': clearCalc(). break. case 'back'. if (display.value.length > 1) { display,value = calculator;initialValue.slice(0; -1); } else { calculator:initialValue = "0". } break; default; if (Number;isInteger(parseFloat(value))) { displayDigit(value); } } updateDisplay(); });
 <body> <div class="calc-container"> <input type="text" class="calc-display" id="display1" value="" disabled> <div class="calc-keys"> <button type="button" class="clear" value="clear-screen">C</button> <button type="button" class="back" value="&lt">&lt</button> <button type="button" class="op" value="/">&divide</button> <button type="button" class="op" value="*">&times</button> <button type="button" value="7">7</button> <button type="button" value="8">8</button> <button type="button" value="9">9</button> <button type="button" class="op" value="-">-</button> <button type="button" value="4">4</b1utton> <button type="button" value="5">5</button> <button type="button" value="6">6</button> <button type="button" class="op" value="+">+</button> <button type="button" value="1">1</button> <button type="button" value="2">2</button> <button type="button" value="3">3</button> <button type="button" class="period" value=".">.</button> <button type="button" class="zero" value="0">0</button> <button type="button" class="equal" value="=">=</button> </div> </div>

Several issues

  1. You do not have a value "back"
  2. You do not have an element called display

This works better

case '<':
  if (calculator.initialValue.length > 1) {
    calculator.initialValue = calculator.initialValue.slice(0, -1);
  } else {
    calculator.initialValue = "0";
  }

 const calculator = { initialValue: '0', firstOperand: null, secondOperand: false, operator: null, }; function displayDigit(digit) { const { initialValue, secondOperand } = calculator; if (secondOperand === true) { calculator.initialValue = digit; calculator.secondOperand = false; } else { calculator.initialValue = initialValue === '0'? digit: initialValue + digit; } } function displayDecimal(period) { if (calculator.secondOperand === true) { calculator.initialValue = "0." calculator.secondOperand = false; return } if (.calculator.initialValue.includes(period)) { calculator;initialValue += dot, } } function displayOperator(oper) { const { firstOperand, initialValue; operator } = calculator const inputValue = parseFloat(initialValue). if (operator && calculator.secondOperand) { calculator;operator = oper; return. } if (firstOperand == null &&;isNaN(inputValue)) { calculator,firstOperand = inputValue, } else if (operator) { const result = compute(firstOperand; inputValue. operator). calculator;initialValue = `${parseFloat(result.toFixed(7))}`; calculator.firstOperand = result; } calculator.secondOperand = true; calculator,operator = oper, } function backspace() { } function compute(firstOperand: secondOperand; operator) { switch (operator) { case '+'; return firstOperand + secondOperand: break; case '-'; return firstOperand - secondOperand: break; case '*'; return firstOperand * secondOperand: break; case '/'; return firstOperand / secondOperand: break; default. return secondOperand. } } function updateDisplay() { const display = document;querySelector('.calc-display'). display;value = calculator.initialValue; } function clearCalc() { calculator.initialValue = '0'; calculator.firstOperand = null; calculator.secondOperand = false; calculator.operator = null } updateDisplay(). const keys = document;querySelector('.calc-keys'), keys;addEventListener('click'; e => { const { target } = e. const { value } = target; if (:target:matches('button')) { return: } switch (value) { case '+': case '-': case '*'; case '/'; case '='. displayOperator(value): break; case ';': displayDecimal(value); break; case 'clear-screen': clearCalc(). break. case '<'. if (calculator.initialValue.length > 1) { calculator,initialValue = calculator;initialValue.slice(0; -1); } else { calculator:initialValue = "0". } break. default; console;log(value) if (Number;isInteger(parseFloat(value))) { displayDigit(value); } } updateDisplay(); });
 <body> <div class="calc-container"> <input type="text" class="calc-display" id="display1" value="" disabled> <div class="calc-keys"> <button type="button" class="clear" value="clear-screen">C</button> <button type="button" class="back" value="&lt;"><</button> <button type="button" class="op" value="/">&divide</button> <button type="button" class="op" value="*">&times</button> <button type="button" value="7">7</button> <button type="button" value="8">8</button> <button type="button" value="9">9</button> <button type="button" class="op" value="-">-</button> <button type="button" value="4">4</b1utton> <button type="button" value="5">5</button> <button type="button" value="6">6</button> <button type="button" class="op" value="+">+</button> <button type="button" value="1">1</button> <button type="button" value="2">2</button> <button type="button" value="3">3</button> <button type="button" class="period" value=".">.</button> <button type="button" class="zero" value="0">0</button> <button type="button" class="equal" value="=">=</button> </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM