我有这个


    <div class="calculator grid">
        <div class="output">
        <div data-previous-operand class="previous-operand"></div>
        <div data-current-operand class="current-operand"></div>
        </div>
        <button data-all-clear>AC</button>
        <button data-delete>DEL</button>
        <button id="innactive" data-operation>()</button>
        <button data-devide data-operation>%</button>
        <button data-number>1</button>
        <button data-number>2</button>
        <button data-number>3</button>
        <button data-operation>^</button>
        <button data-number>4</button>
        <button data-number>5</button>
        <button data-number>6</button>
        <button data-operation>÷</button>
        <button data-number>7</button>
        <button data-number>8</button>
        <button data-number>9</button>
        <button data-operation>*</button>
        <button data-number>.</button>
        <button data-number>0</button>
        <button data-equals id="equals">=</button>
        <button data-operation>+</button>
        <button data-operation id="minus">-</button>
    </div>

还有这个

class Calculator {
  constructor(previousOperandTextElement, currentOperandTextElement, historyLogElement) {
    this.previousOperandTextElement = previousOperandTextElement;
    this.currentOperandTextElement = currentOperandTextElement;
    this.historyLogElement = historyLogElement;
    this.clear();
    // calculator elements
  }
  // clear button function
  clear() {
    this.currentOperand = "";
    this.previousOperand = "";
    this.operation = undefined;
    this.historyLog = ""; // Initialize history log
  }

  delete() {
    this.currentOperand = this.currentOperand.toString().slice(0,-1);
    this.historyLog = this.historyLog.toString().slice(1);

  }

  appendNumber(number) {
    if (number === "." && this.currentOperand.includes(".")) return;
    this.currentOperand = this.currentOperand.toString() + number.toString();
    
    // Prepend the entered number and a newline character to the history log
    this.historyLog = number + this.historyLog;
  }

  chooseOperation(operation) {
    if (this.currentOperand === "") return;
    if (this.previousOperand !== "") {
      this.compute();
    }
    this.operation = operation;
    this.previousOperand = this.currentOperand;
    this.currentOperand = "";
    
    // Prepend the chosen operation and a newline character to the history log
    this.historyLog = operation + '\n' + this.historyLog;
  }
  
  compute() {
    let computation;
    const prev = parseFloat(this.previousOperand);
    const current = parseFloat(this.currentOperand);
    if (isNaN(prev) || isNaN(current)) return;
    switch (this.operation) {
      case "+":
        computation = prev + current;
        break;
      case "-":
        computation = prev - current;
        break;
      case "*":
        computation = prev * current;
        break;
      case "÷":
        computation = prev / current;
        break;
      case "/":
        computation = prev / current;
        break;
      default:
        return;
      case "^":
        computation = Math.pow(prev, current);
        break;
      case "%":
        computation = (prev * 100) / current + "%";
        break;
    }
    this.currentOperand = computation;
    this.operation = undefined;
    this.previousOperand = "";
  }

  getDisplayNumber(number) {
    const stringNumber = number.toString();
    const integerDigits = parseFloat(stringNumber.split(".")[0]);
    const decimalDigits = stringNumber.split(".")[1];
    let integerDisplay;
    if (isNaN(integerDigits)) {
      integerDisplay = "";
    } else {
      integerDisplay = integerDigits.toLocaleString("en", { maximumFractionDigits: 0 });
    }
    if (decimalDigits != null) {
      return `${integerDisplay}.${decimalDigits}`;
    } else {
      return integerDisplay;
    }
  }

  updateDisplay() {
    this.currentOperandTextElement.innerText = this.getDisplayNumber(this.currentOperand);
    if (this.operation != null) {
      this.previousOperandTextElement.innerText = `${this.getDisplayNumber(this.previousOperand)} ${this.operation}`;
    } else {
      this.previousOperandTextElement.innerText = "";
    }
  this.historyLogElement.innerText = this.historyLog;
  }
}


const numberButtons = document.querySelectorAll("[data-number]");
const operationButtons = document.querySelectorAll("[data-operation]");
const equalsButton = document.querySelector("[data-equals]");
const deleteButton = document.querySelector("[data-delete]");
const allClearButton = document.querySelector("[data-all-clear]");
const previousOperandTextElement = document.querySelector("[data-previous-operand]");
const currentOperandTextElement = document.querySelector("[data-current-operand]");
const historyLogElement = document.querySelector("[data-log]");
const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement, historyLogElement);

numberButtons.forEach((button) => {
  button.addEventListener("click", () => {
    calculator.appendNumber(button.innerText);
    calculator.updateDisplay();
  });
});

operationButtons.forEach((button) => {
  button.addEventListener("click", () => {
    calculator.chooseOperation(button.innerText);
    calculator.updateDisplay();
  });
});

equalsButton.addEventListener("click", (button) => {
  calculator.compute();
  calculator.updateDisplay();
});

allClearButton.addEventListener("click", (button) => {
  calculator.clear();
  calculator.updateDisplay();
});

deleteButton.addEventListener("click", (button) => {
  calculator.delete();
  calculator.updateDisplay();
});

document.addEventListener("keydown", function (event) {
  let patternForNumbers = /[0-9]/g;
  let patternForOperators = /[+\-*/]/g;
  if (event.key.match(patternForNumbers)) {
    event.preventDefault();
    calculator.appendNumber(event.key);
    calculator.updateDisplay();
  }
  if (event.key === ".") {
    event.preventDefault();
    calculator.appendNumber(event.key);
    calculator.updateDisplay();
  }
  if (event.key.match(patternForOperators)) {
    event.preventDefault();
    calculator.chooseOperation(event.key);
    calculator.updateDisplay();
  }
  if (event.key === "Enter" || event.key === "=") {
    event.preventDefault();
    calculator.compute();
    calculator.updateDisplay();
  }
  if (event.key === "Backspace") {
    event.preventDefault();
    calculator.delete();
    calculator.updateDisplay();
  }
  if (event.key == "Delete") {
    event.preventDefault();
    calculator.clear();
    calculator.updateDisplay();
  }
});

为什么历史上的数字是以反转方式显示的?我不明白...我一整天都在换房子,我都快疯了

我把这个.CurrentOperand换成了Numbers,希望得到上面的所有数字,以便在历史上显示操作顺序,而不是把所有数字都放在一行,把运算符放在另一行.

推荐答案

用以下操作交换现有日志(log):this.historyLog = this.historyLog + '\n' + operationthis.historyLog = this.historyLog + number

class Calculator {
  constructor(previousOperandTextElement, currentOperandTextElement, historyLogElement) {
    this.previousOperandTextElement = previousOperandTextElement;
    this.currentOperandTextElement = currentOperandTextElement;
    this.historyLogElement = historyLogElement;
    this.clear();
    // calculator elements
  }
  // clear button function
  clear() {
    this.currentOperand = "";
    this.previousOperand = "";
    this.operation = undefined;
    this.historyLog = ""; // Initialize history log
  }

  delete() {
    this.currentOperand = this.currentOperand.toString().slice(0,-1);
    this.historyLog = this.historyLog.toString().slice(1);

  }

  appendNumber(number) {
    if (number === "." && this.currentOperand.includes(".")) return;
    this.currentOperand = this.currentOperand.toString() + number.toString();
    
    // Prepend the entered number and a newline character to the history log
    this.historyLog = this.historyLog + number;
  }

  chooseOperation(operation) {
    if (this.currentOperand === "") return;
    if (this.previousOperand !== "") {
      this.compute();
    }
    this.operation = operation;
    this.previousOperand = this.currentOperand;
    this.currentOperand = "";
    
    // Prepend the chosen operation and a newline character to the history log
    this.historyLog = this.historyLog + '\n' +  operation;
  }
  
  compute() {
    let computation;
    const prev = parseFloat(this.previousOperand);
    const current = parseFloat(this.currentOperand);
    if (isNaN(prev) || isNaN(current)) return;
    switch (this.operation) {
      case "+":
        computation = prev + current;
        break;
      case "-":
        computation = prev - current;
        break;
      case "*":
        computation = prev * current;
        break;
      case "÷":
        computation = prev / current;
        break;
      case "/":
        computation = prev / current;
        break;
      default:
        return;
      case "^":
        computation = Math.pow(prev, current);
        break;
      case "%":
        computation = (prev * 100) / current + "%";
        break;
    }
    this.currentOperand = computation;
    this.operation = undefined;
    this.previousOperand = "";
  }

  getDisplayNumber(number) {
    const stringNumber = number.toString();
    const integerDigits = parseFloat(stringNumber.split(".")[0]);
    const decimalDigits = stringNumber.split(".")[1];
    let integerDisplay;
    if (isNaN(integerDigits)) {
      integerDisplay = "";
    } else {
      integerDisplay = integerDigits.toLocaleString("en", { maximumFractionDigits: 0 });
    }
    if (decimalDigits != null) {
      return `${integerDisplay}.${decimalDigits}`;
    } else {
      return integerDisplay;
    }
  }

  updateDisplay() {
    this.currentOperandTextElement.innerText = this.getDisplayNumber(this.currentOperand);
    if (this.operation != null) {
      this.previousOperandTextElement.innerText = `${this.getDisplayNumber(this.previousOperand)} ${this.operation}`;
    } else {
      this.previousOperandTextElement.innerText = "";
    }
  this.historyLogElement.innerText = this.historyLog;
  }
}


const numberButtons = document.querySelectorAll("[data-number]");
const operationButtons = document.querySelectorAll("[data-operation]");
const equalsButton = document.querySelector("[data-equals]");
const deleteButton = document.querySelector("[data-delete]");
const allClearButton = document.querySelector("[data-all-clear]");
const previousOperandTextElement = document.querySelector("[data-previous-operand]");
const currentOperandTextElement = document.querySelector("[data-current-operand]");
const historyLogElement = document.querySelector("[data-log]");
const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement, historyLogElement);

numberButtons.forEach((button) => {
  button.addEventListener("click", () => {
    calculator.appendNumber(button.innerText);
    calculator.updateDisplay();
  });
});

operationButtons.forEach((button) => {
  button.addEventListener("click", () => {
    calculator.chooseOperation(button.innerText);
    calculator.updateDisplay();
  });
});

equalsButton.addEventListener("click", (button) => {
  calculator.compute();
  calculator.updateDisplay();
});

allClearButton.addEventListener("click", (button) => {
  calculator.clear();
  calculator.updateDisplay();
});

deleteButton.addEventListener("click", (button) => {
  calculator.delete();
  calculator.updateDisplay();
});

document.addEventListener("keydown", function (event) {
  let patternForNumbers = /[0-9]/g;
  let patternForOperators = /[+\-*/]/g;
  if (event.key.match(patternForNumbers)) {
    event.preventDefault();
    calculator.appendNumber(event.key);
    calculator.updateDisplay();
  }
  if (event.key === ".") {
    event.preventDefault();
    calculator.appendNumber(event.key);
    calculator.updateDisplay();
  }
  if (event.key.match(patternForOperators)) {
    event.preventDefault();
    calculator.chooseOperation(event.key);
    calculator.updateDisplay();
  }
  if (event.key === "Enter" || event.key === "=") {
    event.preventDefault();
    calculator.compute();
    calculator.updateDisplay();
  }
  if (event.key === "Backspace") {
    event.preventDefault();
    calculator.delete();
    calculator.updateDisplay();
  }
  if (event.key == "Delete") {
    event.preventDefault();
    calculator.clear();
    calculator.updateDisplay();
  }
});
   <div class="calculator grid">
        <div class="output">
        <div data-previous-operand class="previous-operand"></div>
        <div data-current-operand class="current-operand"></div>
        </div>
        <button data-all-clear>AC</button>
        <button data-delete>DEL</button>
        <button id="innactive" data-operation>()</button>
        <button data-devide data-operation>%</button>
        <button data-number>1</button>
        <button data-number>2</button>
        <button data-number>3</button>
        <button data-operation>^</button>
        <button data-number>4</button>
        <button data-number>5</button>
        <button data-number>6</button>
        <button data-operation>÷</button>
        <button data-number>7</button>
        <button data-number>8</button>
        <button data-number>9</button>
        <button data-operation>*</button>
        <button data-number>.</button>
        <button data-number>0</button>
        <button data-equals id="equals">=</button>
        <button data-operation>+</button>
        <button data-operation id="minus">-</button>
    </div>
    <div data-log></div>

Javascript相关问答推荐

使用CDO时如何将Vue组件存储在html之外?

Express.js:使用Passport.js实现基于角色的身份验证时出现太多重定向问题

仅在React和JS中生成深色

如何解决这个未能在响应上执行json:body stream已读问题?

*ngFor和@代表输入decorator 和选角闭合

硬币兑换运行超时

格式值未保存在redux持久切片中

为什么promise对js中的错误有一个奇怪的优先级?

构造HTML表单以使用表单数据创建对象数组

在带有背景图像和圆形的div中添加长方体阴影时的重影线

JSDoc创建并从另一个文件导入类型

Html文件和客户端存储的相关问题,有没有可能?

如何在文本字段中输入变量?

元素字符串长度html

是否可以在Photoshop CC中zoom 路径项?

一个实体一刀VS每个实体多刀S

使每个<;li>;元素的 colored颜色 与随机生成的 colored颜色 列表不同(不重复

Django导入问题,无法导入我的应用程序,但我已在设置中安装了它

我的NavLink活动类在REACT-ROUTER-V6中出现问题

$GTE的mongoose 问题