簡體   English   中英

計算后清除計算器顯示javascript

[英]Clear calculator display javascript after calculation

我正在 JavaScript 中做一個初學者計算器項目,我試圖找出一種在新計算開始時清除顯示的方法。 例如:

  • 按“1 + 1”
  • 按“=”
  • 顯示現在將是“2”

如果我現在想開始一個新的計算並且我按下 3 例如,這將 append 到結果即顯示現在將是“23”。

開始新計算時如何強制顯示刪除結果?

我想知道是否可以使用 if 語句來做到這一點:

function resetDisplay() {
  if(//previous key pressed was "=") {
    on next key press, remove calculation result and only show new input
};

這是我的 JS 代碼

//function that displays values in the calculator window
function disp(val) {
  document.getElementById("display").value+=val;
}

//function for clearing the display
function clr() {
  document.getElementById("display").value="";
}

//function for backspace
function del() {
  let dis = document.getElementById("display").value;
  document.getElementById("display").value = dis.substring(0, dis.length - 1);
}

//function for evaluation
function solve() {
  let numericalExpression = document.getElementById("display").value;
  let result = eval(numericalExpression);
  document.getElementById("display").value = result;
}

//function to clear display after calculation

function resetDisplay() {}

還有codepen上整個項目的鏈接

如何准確定義新計算的開始? 基本上我希望在按下“=”按鈕后按下該鍵以刪除先前的計算。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM