繁体   English   中英

我有问题。 在计算器中。 我只需要一个点

[英]I have a problem with my . in calculator. I need to have just one dot

只需要一个点,这样计算器就可以正常工作。 没有像我现在这样的很多点。 现在这是一团糟。 我稍后会在代码中处理其他内容,但现在我被困在这里了。

 function dec(d) {

      let display = document.getElementById('display');
      let displayArray = display.innerHTML.split("");
      displayArray += d.innerHTML;
      display.innerHTML = displayArray;
      if (!displayArray.includes(".")) {
          displayArray += ".";
          displayArray = display;
      }

我尝试了一些东西,现在我无处可去。

我需要正常,只需一个点,现在因为数组我有“,”。 每一个“。” 我点击了

我现在问题是用数组,拆分数组或其他东西做某事但不知道问题的确切位置。

问题是数组与字符串的连接

displayArray += d.innerHTML;

字符串转换/默认情况下数组的toString用','连接所有元素

如果你想要任何其他分隔符,你将不得不定义它

 let array = [1,2,3,4] console.log(array+'') console.log(array.join()) console.log(array.toString()) //Correct one console.log(array.join('')) 

所以你的代码必须是

displayArray = displayArray.join('') + d.innerHTML;

但是displayArray将不再是一个数组而是一个String

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Calculator</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>


  <script src="script.js"></script>



    <table class="main" id="gradient">
      <tr class="row">
        <th class="column displayValue" id="display">0</th>
      </tr>
      <tr class="row">
        <th class="buttons bckbtn" onclick="clearAll()">C</th>
        <th class="buttons bckbtn" onclick="backbutton(this)">backspace</th>
      </tr>
      <tr class="row">
        <th class="buttons" onclick="numbers(this)">7</th>
        <th class="buttons" onclick="numbers(this)">8</th>
        <th class="buttons" onclick="numbers(this)">9</th>
        <th class="buttons">/</th>
      </tr>
      <tr class="row">
        <th class="buttons" onclick="numbers(this)">4</th>
        <th class="buttons" onclick="numbers(this)">5</th>
        <th class="buttons" onclick="numbers(this)">6</th>
        <th class="buttons">x</th>
      </tr>
      <tr class="row">
        <th class="buttons" onclick="numbers(this)">1</th>
        <th class="buttons" onclick="numbers(this)">2</th>
        <th class="buttons" onclick="numbers(this)">3</th>
        <th class="buttons" onclick="numbers(this)">-</th>
      </tr>
      <tr class="row">
        <th class="buttons" id="decimal" onclick="dec(this)">.</th>
        <th class="buttons" onclick="numbers(this)">0</th>
        <th class="buttons" onclick="numbers(this)">=</th>
        <th class="buttons" onclick="numbers(this)">+</th>
      </tr>
    </table>


  </body>
</html>



function numbers(el) {

  let display = document.getElementById('display');

  if (display.innerHTML === "0") {
   display.innerHTML = "";
   display.innerHTML += el.innerHTML;
  }
  else {
    display.innerHTML += el.innerHTML;
  }
}


function clearAll() {

  let display = document.getElementById('display');
  display.innerHTML = "0";

}


function backbutton(b) {

  let display = document.getElementById('display');
  let value = display.innerHTML;
  let newValue = value.substr(0, value.length - 1);
  display.innerHTML = newValue;
  if (display.innerHTML === "") {
    display.innerHTML = "0";
  }
  document.getElementById("decimal").setAttribute("onclick", "dec(this)");

}


function dec(d) {

  let display = document.getElementById('display');
  let displayArray = display.innerHTML.split("");
  displayArray.join("") += d.innerHTML;
  display.innerHTML = displayArray;
  if (!displayArray.includes(".")) {
      displayArray += ".";
      displayArray = display;
  }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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