什么阻止我的javascript表单运行?

时间:2015-03-07 22:08:23

标签: javascript

我正在为计算机课程编写简介。分配是创建一个带有计算器的网页,该计算器将十进制数转换为二进制数,转换为补码数,并转换为二进制数。我已经让二进制函数运行得很好......但是在编码之后我认为对于补充函数有效...一切都停止了工作。任何正确方向的提示都会非常有用!这是我的代码:

function decimalToBinary() {
  var decimal;
  var decimalInput = document.getElementById("numberInput").value;
  decimal = Number(decimalInput);
  var binary = decimal.toString(2);
  while (binary.length < 8) {
    binary = "0" + binary;
  }

  //var oneComp = onesComplement(binary);
  var prettyString = binary.substring(0, 4) + " " + binary.substring(4);
  prettyString = decimal.toString() + " decimal is " + prettyString;
  prettyString = prettyString + " binary.";
  document.getElementById("answerString").innerHTML = prettyString;
  var prettyString2 = oneComp.substring(0, 4) + " " + oneComp.substring(4);
  prettyString2 = oneComp.toString() + " one's complement is " + prettyString2;
  document.getElementById("answerString2").innerHTML = prettyString2;
}

function onesComplement(binaryNumberString) {
  for (i = 0; i < 8; i++) {
    if binaryNumberString.substring(i, 1) == 0 {
      binaryNumberString.substring(i, 1) = 1;
    } else {
      binaryNumberString.substring(i, 1) = 0;
    }
    return binaryNumberString;
  }
}

另外,我的页面的html是这样编写的,用于获取输入:

<body>
    <input type = "text" id="numberInput" />
    <button onClick = "decimalToBinary()">Convert</button>
    <p id="answerString">The answer will be here</p>
    <p id="answerString2">Second Answer</p>
</body>

2 个答案:

答案 0 :(得分:0)

在函数onesComplement中,您缺少if语句中的括号。

if (binaryNumberString.substring(i, 1) == 0) {

答案 1 :(得分:0)

这是你的脚本修复:

<body>
    <script>
        function decimalToBinary() {
            var decimal;
            var decimalInput = document.getElementById("numberInput").value;
            decimal = Number(decimalInput);
            var binary = decimal.toString(2);
            while (binary.length < 8) {
                binary = "0" + binary;
            }

            var oneComp = onesComplement(binary);
            var prettyString = binary.substring(0, 4) + " " + binary.substring(4);
            prettyString = decimal.toString() + " decimal is " + prettyString;
            prettyString = prettyString + " binary.";
            document.getElementById("answerString").innerHTML = prettyString;
            var prettyString2 = oneComp.substring(0, 4) + " " + oneComp.substring(4);
            prettyString2 = oneComp.toString() + " one's complement is " + prettyString2;
            document.getElementById("answerString2").innerHTML = prettyString2;
        }

        function onesComplement(binaryNumberString) {
            var binNum = '';
            for (i = 0; i < 8; i++) {

                if (binaryNumberString.substring(i, 1) == 0) {
                    binNum = binNum + '1';
                } else {
                    binNum = binNum + '0';
                }
                return binNum;
            }
        }
    </script>
    <input type="text" id="numberInput" />
    <button onClick="decimalToBinary()">Convert</button>
    <p id="answerString">The answer will be here</p>
    <p id="answerString2">Second Answer</p>
</body>

您遇到了一些错误,var已注释掉,缺少括号,并且您尝试将值设置为函数调用。查看代码并比较您是否会发现问题。

我并不是说这是实现目标的最佳方式,我只是修改了您提供的代码。