计算器中的减法和加法结果不正确

时间:2019-03-26 11:22:25

标签: javascript html

我正在使用HTMLJavascript创建一个for循环计算器,我尝试了一切。问题在于减法的结果不正确,与加法相同。

这是功能

<script>
    var num1 = 0, num2 = 0, resultt = 1, resultt = 1;
    var init = 0, inc = 0, imput = 0;
    function gen() {
        imput = parseInt(document.getElementById("txt1").value);
        for(init = 1; init <= imput; init++) {
            document.write("<input type='text' value='" + init + "' id='" + init + "'><br>");
        }

        document.write("<input type='button' value='compute'onclick='computee();'><div style='border: 1px solid black;' id='displayy'></div></br>");
        document.write("<input type='button' value='subtract'onclick='subtractt();'><div style='border: 1px solid black;' id='displayy'></div></br>");
        document.write("<input type='button' value='division'onclick='divisionn();'><div style='border: 1px solid black;' id='displayy'></div></br>");
        document.write("<input type='button' value='multiplication'onclick='multiplicationn();'><div style='border: 1px solid black;' id='displayy'></div></br>");      
    }

这是另一半

    function computee() {
        var imputt = "";
        for(inc = 1; inc <= init - 1; inc++) {
            resultt = resultt + parseInt(document.getElementById(inc).value);
        }

        document.getElementById("displayy").innerHTML = resultt;
    }

    function subtractt() {
        var imputt = "";
        for(inc = 1; inc <= init - 1; inc++) {
            resultt = resultt - parseInt(document.getElementById(inc).value);
        }

        document.getElementById("displayy").innerHTML = resultt;
    }

    function divisionn() {
        var imputt = "";
        for(inc = 1; inc <= init - 1; inc++) {
            resultt = resultt % parseInt(document.getElementById(inc).value);
        }
        document.getElementById("displayy").innerHTML = resultt;
    }

    function multiplicationn() {
        var imputt = "";
        for(inc = 1; inc <= init - 1; inc++) {
            resultt = resultt * parseInt(document.getElementById(inc).value);
        }
        document.getElementById("displayy").innerHTML = resultt;
    }

</script>
</head>
    <body>
        <input type="number" id="txt1" placeholder="enter number of textboxes" onkeyup="solve();">
        <input type="button" value="generate" onclick="gen()">
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

1-您的div输出具有相同的ID,并且2- ParseInt的乘法和除法必须为parseFloat

 var num1 = 0, num2 = 0, resultt = 1, resultt = 1;
            var init = 0, inc = 0, imput = 0;
            function gen()
            {
                imput = parseInt(document.getElementById("txt1").value);
                for(init = 1; init <= imput; init++)
                {
                document.write("<input type='text' value='" + init + "' id='" + init + "'><br>");
                }
                document.write("<input type='button' value='compute'onclick='computee();'><div style='border: 1px solid black;' id='displayy1'></div></br>");
                document.write("<input type='button' value='subtract'onclick='subtractt();'><div style='border: 1px solid black;' id='displayy2'></div></br>");
                document.write("<input type='button' value='division'onclick='divisionn();'><div style='border: 1px solid black;' id='displayy3'></div></br>");
                document.write("<input type='button' value='multiplication'onclick='multiplicationn();'><div style='border: 1px solid black;' id='displayy4'></div></br>");      
            }
                function computee()
        {
        var imputt = "";
        for(inc = 1; inc <= init - 1; inc++)
        {
            resultt = resultt + parseInt(document.getElementById(inc).value);
        }
        document.getElementById("displayy1").innerHTML = resultt;
        }
        function subtractt()
        {
        var imputt = "";
        for(inc = 1; inc <= init - 1; inc++)
        {
            resultt = resultt - parseInt(document.getElementById(inc).value);
        }
        document.getElementById("displayy2").innerHTML = resultt;
        }
        function divisionn()
        {
        var imputt = "";
        for(inc = 1; inc <= init - 1; inc++)
        {
            resultt = resultt / parseFloat(document.getElementById(inc).value);
        }
        document.getElementById("displayy3").innerHTML = resultt;
        }
        function multiplicationn()
        {
        var imputt = "";
        for(inc = 1; inc <= init - 1; inc++)
        {
            resultt = resultt * parseFloat(document.getElementById(inc).value);
        }
        document.getElementById("displayy4").innerHTML = resultt;
        }
<input type="number" id="txt1" placeholder="enter number of textboxes">
 <input type="button" value="generate" onclick="gen()">

答案 1 :(得分:0)

我至少认为我做了你想要的。

请继续看一下:

const input = document.getElementById('input');

input.addEventListener('input', e => calculateStuff(e.target.value));

function calculateStuff(numb) {
  let addition = 1;
  let subtraction = 1;
  let multiplication = 1;
  let division = 1;
  for(let i = 2; i <= parseInt(numb); i++) {
  	addition += i;
    subtraction -= i;
    multiplication *= i;
    division /= i;
  }
  
  console.log('// *********************** //');
  console.log('Addition:', addition);
  console.log('Subtraction:', subtraction);
  console.log('Multiplicaton:', multiplication);
  console.log('Division:', division);
  console.log('// *********************** //');
}
Input number: <input type="number" id="input" />

我为每个操作预先分配了1,否则我将零除以1,依此类推...这就是循环然后从2开始的原因。否则,代码是自我解释的,至少在我看来,所以如果您需要一些说明让我知道:)