JavaScript计算器-仅一个按钮不起作用(平方根)

时间:2018-11-26 12:35:03

标签: javascript

我找不到使 Math.sqrt(value)功能起作用的方法。 (╯°□°)╯┻━┻ 我不确定,但是问题似乎出在 operators 上。 我还尝试将函数添加到const calculation 内,但该方法也不起作用。

我不想在HTML中设置onclick来调用该函数。我试图在整个代码中保持相同的逻辑。但是我不知道如何解决这个问题。

Ps ::我建议复制文件并在Safari或Firefox中进行测试。计算器看起来不错。我试图创建一个与macOS Calculator similar非常相似的版本。

const calculator = {
  displayValue: "0",
  firstOperand: null,
  waitSecondOperand: false,
  operator: null
};

function inputDigit(digit) {
  const { displayValue, waitSecondOperand } = calculator;

  if (waitSecondOperand === true) {
    calculator.displayValue = digit;
    calculator.waitSecondOperand = false;
  } else {
    calculator.displayValue =
      displayValue === "0" ? digit : displayValue + digit;
  }

  console.log(calculator);
}

function decimal(dot) {
  if (calculator.waitSecondOperand === true) return;

  if (!calculator.displayValue.includes(dot)) {
    calculator.displayValue += dot;
  }
}

function inputDigit(digit) {
  const { displayValue, waitSecondOperand } = calculator;

  if (waitSecondOperand === true) {
    calculator.displayValue = digit;
    calculator.waitSecondOperand = false;
  } else {
    calculator.displayValue =
      displayValue === "0" ? digit : displayValue + digit;
  }
  console.log(calculator);
}

function operators(nextOperator) {
  const { firstOperand, displayValue, operator } = calculator;
  const inputValue = parseFloat(displayValue);

  if (operator && calculator.waitSecondOperand) {
    calculator.operator = nextOperator;
    console.log(calculator);
    return;
  }

  if (firstOperand == null) {
    calculator.firstOperand = inputValue;
  } else if (operator) {
    const currentValue = firstOperand || 0;
    const result = calculation[operator](currentValue, inputValue);

    calculator.displayValue = String(result);
    calculator.firstOperand = result;
  }

  calculator.waitSecondOperand = true;
  calculator.operator = nextOperator;
  console.log(calculator);
}

const calculation = {

  "%": (firstOperand, secondOperand) => (firstOperand * secondOperand) / 100,

  "/": (firstOperand, secondOperand) => firstOperand / secondOperand,

  "*": (firstOperand, secondOperand) => firstOperand * secondOperand,

  "+": (firstOperand, secondOperand) => firstOperand + secondOperand,

  "-": (firstOperand, secondOperand) => firstOperand - secondOperand,

  "=": (firstOperand, secondOperand) => secondOperand
};

// I can not figure out how to make this function work.
// Below the source of my despair.

const getSqrt = document.querySelector(".sqrt");
getSqrt.addEventListener("click", firstOperand => {
  calculator.displayValue = Math.sqrt(firstOperand);
});

function resetCalculator() {
  calculator.displayValue = "0";
  calculator.firstOperand = null;
  calculator.waitingForSecondOperand = false;
  calculator.operator = null;
  console.log(calculator);
}

function updateDisplay() {
  const display = document.querySelector(".calculator--display");
  display.value = calculator.displayValue;
}

updateDisplay();


const buttons = document.querySelector(".calculator--keys");
buttons.addEventListener("click", e => {
  
  const { target } = e;
  if (!target.matches("button")) {
    return;
  }
  if (target.classList.contains("operator")) {
    operators(target.value);
    updateDisplay();
    return;
  }
  if (target.classList.contains("decimal")) {
    decimal(target.value);
    updateDisplay();
    return;
  }
  if (target.classList.contains("clear")) {
    resetCalculator();
    updateDisplay();
    return;
  }
  inputDigit(target.value);
  updateDisplay();
});
@import url("https://fonts.googleapis.com/css?family=Jura:500");
*,
*:after,
*:before {
  margin: 0px;
  padding: 0px;
  border-spacing: 0;
  border-collapse: collapse;
  box-sizing: border-box;
  border: none;
  box-sizing: border-box;
}
html {
  height: 100%;
  background: #655f5c;
  box-sizing: border-box;
}
.container {
  width: 375px;
  height: 564px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}
button,
input {
  color: #fff;
  font-family: "Jura", sans-serif;
  font-weight: 500;
}
.calculator {
  background: #312f2e;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}
.calculator--keys {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.calculator--keys > tr {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}
.calculator--keys tr > td {
  width: 25%;
}
table tr:first-child button {
  background-color: #605c57;
}
.calculator--display,
.calculator--keys__button {
  color: #fff;
  padding: 10px;
}
.calculator--display {
  background-color: #312f2e;
  border: none;
  color: #fff;
  height: 95.2px;
  width: 100%;
  font-size: 1.8em;
  text-align: right;
}
.calculator--keys__button {
  display: flex;
  border: solid #312f2e;
  border-width: 0.01em;
  cursor: pointer;
  font-size: 1.5em;
  width: 100%;
  height: 93.75px;
  background-color: #8a8783;
  justify-content: center;
  align-items: center;
  text-align: center;
  transition: all 0.03s;
}
.calculator--keys__button:focus,
.calculator--keys__button:active {
  outline: none;
}
.press {
  transition: all 0.03s;
  transform: scale(0.98);
}
.zero {
  -webkit-flex-grow: 2;
  flex-grow: 2;
}
.orange {
  font-size: 2em;
  background-color: #ff9f0c !important;
}
<!DOCTYPE html>
<html lang="pt-br">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="./css/styles.css" />
    <title>Calculadora</title>
  </head>
  <body>
    <section>
      <div class="container">
        <div class="calculator flex">
          <input
          type="text"
          class="calculator--display"
          name="calculator--display"
          value=""
          disabled
          />
        <table>
          <tbody  class="calculator--keys">
            <tr>
              <td><button type="button" class="calculator--keys__button key clear" value="clear">C</button></td>
              <td><button type="button" class="calculator--keys__button operator sqrt" value="sqrt">&radic;</button></td>
              <td><button type="button" class="calculator--keys__button operator perc" value="%">%</button></td>
              <td><button type="button" class="calculator--keys__button operator orange" value="/">&divide;</button></td>
            </tr>
            <tr>
              <td><button type="button" class="calculator--keys__button" value="7">7</button></td>
              <td><button type="button" class="calculator--keys__button" value="8">8</button></td>
              <td><button type="button" class="calculator--keys__button" value="9">9</button></td>
              <td><button type="button" class="calculator--keys__button operator orange" value="*">&times;</button></td>
            </tr>
            <tr>
              <td><button type="button" class="calculator--keys__button" value="4">4</button></td>
              <td><button type="button" class="calculator--keys__button" value="5">5</button></td>
              <td><button type="button" class="calculator--keys__button" value="6">6</button></td>
              <td><button type="button" class="calculator--keys__button operator orange" value="-">-</button></td>
            </tr>
            <tr>
              <td><button type="button" class="calculator--keys__button" value="1">1</button></td>
              <td><button type="button" class="calculator--keys__button" value="2">2</button></td>
              <td><button type="button" class="calculator--keys__button" value="3">3</button></td>
              <td><button type="button" class="calculator--keys__button operator orange" value="+">+</button></td>
            </tr>
            <tr>
              <td class="zero"><button type="button" class="calculator--keys__button" value="0">0</button></td>
              <td><button type="button" class="calculator--keys__button decimal" value=".">.</button></td>
              <td><button type="button" class="calculator--keys__button equal operator orange" value="=">=</button></td>
            </tr>
          </tbody>
        </table>
      </div>
    </section>
    <script src="./js/build.js"></script>
  </body>
</html>

我每天都在尝试开发一些东西,以成为更好的开发人员。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

const calculator = {
  displayValue: "0",
  firstOperand: null,
  waitSecondOperand: false,
  operator: null
};

function inputDigit(digit) {
  const { displayValue, waitSecondOperand } = calculator;

  if (waitSecondOperand === true) {
    calculator.displayValue = digit;
    calculator.waitSecondOperand = false;
  } else {
    calculator.displayValue =
      displayValue === "0" ? digit : displayValue + digit;
  }

  console.log(calculator);
}

function decimal(dot) {
  if (calculator.waitSecondOperand === true) return;

  if (!calculator.displayValue.includes(dot)) {
    calculator.displayValue += dot;
  }
}

function inputDigit(digit) {
  const { displayValue, waitSecondOperand } = calculator;

  if (waitSecondOperand === true) {
    calculator.displayValue = digit;
    calculator.waitSecondOperand = false;
  } else {
    calculator.displayValue =
      displayValue === "0" ? digit : displayValue + digit;
  }
  console.log(calculator);
}

function operators(nextOperator) {
  const { firstOperand, displayValue, operator } = calculator;
  const inputValue = parseFloat(displayValue);

  if (operator && calculator.waitSecondOperand) {
    calculator.operator = nextOperator;
    console.log(calculator);
    return;
  }

  if (firstOperand == null) {
    calculator.firstOperand = inputValue;
  } else if (operator) {
    const currentValue = firstOperand || 0;
    const result = calculation[operator](currentValue, inputValue);

    calculator.displayValue = String(result);
    calculator.firstOperand = result;
  }

  calculator.waitSecondOperand = true;
  calculator.operator = nextOperator;
  console.log(calculator);
}

const calculation = {

  "%": (firstOperand, secondOperand) => (firstOperand * secondOperand) / 100,

  "/": (firstOperand, secondOperand) => firstOperand / secondOperand,

  "*": (firstOperand, secondOperand) => firstOperand * secondOperand,

  "+": (firstOperand, secondOperand) => firstOperand + secondOperand,

  "-": (firstOperand, secondOperand) => firstOperand - secondOperand,

  "=": (firstOperand, secondOperand) => secondOperand
};

// I can not figure out how to make this function work.
// Below the source of my despair.

const getSqrt = document.querySelector(".sqrt");
getSqrt.addEventListener("click", firstOperand => {
  calculator.displayValue = Math.sqrt(calculator.displayValue);
});

function resetCalculator() {
  calculator.displayValue = "0";
  calculator.firstOperand = null;
  calculator.waitingForSecondOperand = false;
  calculator.operator = null;
  console.log(calculator);
}

function updateDisplay() {
  const display = document.querySelector(".calculator--display");
  display.value = calculator.displayValue;
}

updateDisplay();


const buttons = document.querySelector(".calculator--keys");
buttons.addEventListener("click", e => {
  
  const { target } = e;
  if (!target.matches("button")) {
    return;
  }
  if (target.classList.contains("operator")) {
    operators(target.value);
    updateDisplay();
    return;
  }
  if (target.classList.contains("decimal")) {
    decimal(target.value);
    updateDisplay();
    return;
  }
  if (target.classList.contains("clear")) {
    resetCalculator();
    updateDisplay();
    return;
  }
  inputDigit(target.value);
  updateDisplay();
});
@import url("https://fonts.googleapis.com/css?family=Jura:500");
*,
*:after,
*:before {
  margin: 0px;
  padding: 0px;
  border-spacing: 0;
  border-collapse: collapse;
  box-sizing: border-box;
  border: none;
  box-sizing: border-box;
}
html {
  height: 100%;
  background: #655f5c;
  box-sizing: border-box;
}
.container {
  width: 375px;
  height: 564px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}
button,
input {
  color: #fff;
  font-family: "Jura", sans-serif;
  font-weight: 500;
}
.calculator {
  background: #312f2e;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}
.calculator--keys {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.calculator--keys > tr {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}
.calculator--keys tr > td {
  width: 25%;
}
table tr:first-child button {
  background-color: #605c57;
}
.calculator--display,
.calculator--keys__button {
  color: #fff;
  padding: 10px;
}
.calculator--display {
  background-color: #312f2e;
  border: none;
  color: #fff;
  height: 95.2px;
  width: 100%;
  font-size: 1.8em;
  text-align: right;
}
.calculator--keys__button {
  display: flex;
  border: solid #312f2e;
  border-width: 0.01em;
  cursor: pointer;
  font-size: 1.5em;
  width: 100%;
  height: 93.75px;
  background-color: #8a8783;
  justify-content: center;
  align-items: center;
  text-align: center;
  transition: all 0.03s;
}
.calculator--keys__button:focus,
.calculator--keys__button:active {
  outline: none;
}
.press {
  transition: all 0.03s;
  transform: scale(0.98);
}
.zero {
  -webkit-flex-grow: 2;
  flex-grow: 2;
}
.orange {
  font-size: 2em;
  background-color: #ff9f0c !important;
}
<!DOCTYPE html>
<html lang="pt-br">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="./css/styles.css" />
    <title>Calculadora</title>
  </head>
  <body>
    <section>
      <div class="container">
        <div class="calculator flex">
          <input
          type="text"
          class="calculator--display"
          name="calculator--display"
          value=""
          disabled
          />
        <table>
          <tbody  class="calculator--keys">
            <tr>
              <td><button type="button" class="calculator--keys__button key clear" value="clear">C</button></td>
              <td><button type="button" class="calculator--keys__button operator sqrt" value="sqrt">&radic;</button></td>
              <td><button type="button" class="calculator--keys__button operator perc" value="%">%</button></td>
              <td><button type="button" class="calculator--keys__button operator orange" value="/">&divide;</button></td>
            </tr>
            <tr>
              <td><button type="button" class="calculator--keys__button" value="7">7</button></td>
              <td><button type="button" class="calculator--keys__button" value="8">8</button></td>
              <td><button type="button" class="calculator--keys__button" value="9">9</button></td>
              <td><button type="button" class="calculator--keys__button operator orange" value="*">&times;</button></td>
            </tr>
            <tr>
              <td><button type="button" class="calculator--keys__button" value="4">4</button></td>
              <td><button type="button" class="calculator--keys__button" value="5">5</button></td>
              <td><button type="button" class="calculator--keys__button" value="6">6</button></td>
              <td><button type="button" class="calculator--keys__button operator orange" value="-">-</button></td>
            </tr>
            <tr>
              <td><button type="button" class="calculator--keys__button" value="1">1</button></td>
              <td><button type="button" class="calculator--keys__button" value="2">2</button></td>
              <td><button type="button" class="calculator--keys__button" value="3">3</button></td>
              <td><button type="button" class="calculator--keys__button operator orange" value="+">+</button></td>
            </tr>
            <tr>
              <td class="zero"><button type="button" class="calculator--keys__button" value="0">0</button></td>
              <td><button type="button" class="calculator--keys__button decimal" value=".">.</button></td>
              <td><button type="button" class="calculator--keys__button equal operator orange" value="=">=</button></td>
            </tr>
          </tbody>
        </table>
      </div>
    </section>
    <script src="./js/build.js"></script>
  </body>
</html>

假设您希望人们在单击之前已经选择了一个数字 sqrt按钮,您只需要更改一行:

calculator.displayValue = Math.sqrt(firstOperand); calculator.displayValue = Math.sqrt(calculator.displayValue);

在第一种情况下,firstOperand保留一个鼠标单击事件,该事件将不作为Math.sqrt的参数

传入calculator.displayValue将作为其当前选择的数字