为什么我的JS Calc中不能放一个点以上

时间:2019-03-06 04:24:01

标签: javascript

我正在制造计算器,但是...我可以在第一个数字上加上两个点,但不能在第二个数字上加上点。我想念一些东西,但不知道。

我尝试了不同的方法,但无济于事...

也许我不得不尝试另一种方式来做,或者我错过了点的某些条件。而另一件事是,我无法使用“退格键”删除一个数字,而使用了“ del”。

function insert(num) {
  const lastChar = document.form.textview.value;

  if ((!document.form.textview.value || isNaN(lastChar)) && isNaN(num)) {
    return '';
  }

  document.form.textview.value += num;
};

function equal() {
  let exp = document.form.textview.value;
  if (exp) {
    document.form.textview.value = eval(exp);
    return '';
  }
};

function clean() {
  document.form.textview.value = "";
}

function back() {
  let exp = document.form.textview.value;
  document.form.textview.value = exp.substring(0, exp.length - 1);
  return '';
}

document.addEventListener('keydown', function(n) {
  let stringChar = n.key;
  let numChar = Number(n);

  switch (stringChar) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '-':
    case '+':
    case '/':
    case '*':
      insert(stringChar);
      break;
    case '.':
      let text = document.form.textview.value;
      if (!text.includes('.') && text.length) {
        document.form.textview.value += '.';
      } else if (text.includes('.') && (text.indexOf('.') <= text.length - 1)) {
        document.form.textview.value += '.';
      }
      break;
    case 'Enter':
      equal(numChar);
      break;
    case 'Delete':
      back(numChar);
      break;
  }

});
body {
  padding: 0;
  margin: 0;
  background: #c0c0c0;
}

.button {
  width: 50px;
  height: 50px;
  font-size: 22px;
  margin: 3px;
  cursor: pointer;
  border-radius: 5px;
  background: #e6e6e6;
  box-shadow: 0 0 2px 2px rgba(0, 0, 0, .5);
}

.button:hover {
  background: #cecece;
}

.button:focus {
  outline: 0;
}

.btn {
  width: 110px;
}

.textview {
  width: 218px;
  margin: 5px;
  font-size: 26px;
  padding: 10px 5px;
  box-shadow: inset 0 0 2px 2px rgba(0, 0, 0, .5);
  border-radius: 6px;
  background: #e6e6e6;
  color: rgb(0, 0, 0);
}

.main {
  box-shadow: 0 0 10px 10px rgba(0, 0, 0, 0.5);
  border-radius: 5px;
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  background: #858585;
}
<body>
  <div class="main">
    <form name="form">
      <input class="textview" name="textview" disabled>
    </form>

    <table>
      <tr>
        <td><input class="button" type="button" value="CE" onclick="insert(clean())"></td>
        <td><input class="button" type="button" value="C" onclick="insert(clean())"></td>
        <td><input class="button" type="button" value="<" onclick="insert(back())"></td>
        <td><input class="button" type="button" value="/" onclick="insert('/')"></td>
      </tr>
      <tr>
        <td><input class="button" type="button" value="7" onclick="insert(7)"></td>
        <td><input class="button" type="button" value="8" onclick="insert(8)"></td>
        <td><input class="button" type="button" value="9" onclick="insert(9)"></td>
        <td><input class="button" type="button" value="*" onclick="insert('*')"></td>
      </tr>
      <tr>
        <td><input class="button" type="button" value="4" onclick="insert(4)"></td>
        <td><input class="button" type="button" value="5" onclick="insert(5)"></td>
        <td><input class="button" type="button" value="6" onclick="insert(6)"></td>
        <td><input class="button" type="button" value="-" onclick="insert('-')"></td>
      </tr>
      <tr>
        <td><input class="button" type="button" value="1" onclick="insert(1)"></td>
        <td><input class="button" type="button" value="2" onclick="insert(2)"></td>
        <td><input class="button" type="button" value="3" onclick="insert(3)"></td>
        <td><input class="button" type="button" value="+" onclick="insert('+')"></td>
      </tr>
      <tr>
        <td colspan="2"><input class="button btn" type="button" value="0" onclick="insert(0)"></td>
        <td><input class="button" type="button" value="." onclick="insert('.')"></td>
        <td><input class="button" type="button" value="=" onclick="insert(equal())"></td>
      </tr>
    </table>
  </div>

</body>

1 个答案:

答案 0 :(得分:1)

例如,当您放置两个点isNaN(lastChar)并且isNaN(num)true时,insert(num)返回并且不打印。您可以使用eval检查语法try catch。如果catch不打印“ not number char”,否则不打印.。如果.的最后一个索引大于 char的最后一个索引而不是数字和. ,例如1.21.2+1.,则您不会导入{ {1}}。对不起,我的英语不好。试试吧:

.
function insert(num) {
  const lastChar = document.form.textview.value;
  if(lastChar){
    try {
        eval(lastChar);
        if(lastIndexNotNumberCharInString(lastChar) < lastChar.lastIndexOf('.') && num == '.'){
            return '';
        }
    } catch (e) {
        if (isNaN(num)) {
           return '';
        }
    }
  }else{
     if(isNaN(num)){
         return '';
     }
  }

  document.form.textview.value += num;
};

function lastIndexNotNumberCharInString(char){
    var j = -1;
    for (var i = 0; i < char.length; i++) {
      if(isNaN(char.charAt(i)) && char.charAt(i) != '.'){
         j = i;
      }
    }
    return j;
}

function equal() {
  let exp = document.form.textview.value;
  if (exp) {
    document.form.textview.value = eval(exp);
    return '';
  }
};

function clean() {
  document.form.textview.value = "";
}

function back() {
  let exp = document.form.textview.value;
  document.form.textview.value = exp.substring(0, exp.length - 1);
  return '';
}

document.addEventListener('keydown', function(n) {
  let stringChar = n.key;
  let numChar = Number(n);

  switch (stringChar) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '-':
    case '+':
    case '/':
    case '*':
      insert(stringChar);
      break;
    case '.':
      let text = document.form.textview.value;
      if (!text.includes('.') && text.length) {
        document.form.textview.value += '.';
      } else if (text.includes('.') && (text.indexOf('.') <= text.length - 1)) {
        document.form.textview.value += '.';
      }
      break;
    case 'Enter':
      equal(numChar);
      break;
    case 'Delete':
      back(numChar);
      break;
  }

});
body {
  padding: 0;
  margin: 0;
  background: #c0c0c0;
}

.button {
  width: 50px;
  height: 50px;
  font-size: 22px;
  margin: 3px;
  cursor: pointer;
  border-radius: 5px;
  background: #e6e6e6;
  box-shadow: 0 0 2px 2px rgba(0, 0, 0, .5);
}

.button:hover {
  background: #cecece;
}

.button:focus {
  outline: 0;
}

.btn {
  width: 110px;
}

.textview {
  width: 218px;
  margin: 5px;
  font-size: 26px;
  padding: 10px 5px;
  box-shadow: inset 0 0 2px 2px rgba(0, 0, 0, .5);
  border-radius: 6px;
  background: #e6e6e6;
  color: rgb(0, 0, 0);
}

.main {
  box-shadow: 0 0 10px 10px rgba(0, 0, 0, 0.5);
  border-radius: 5px;
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  background: #858585;
}