将文本框中的数字分成4组数字,每组数字得到它的总和,并组合每组的每个总和的所有最后数字

时间:2018-05-19 21:53:53

标签: javascript html



<html>
<title> Test </title>

<body>
  <b>Paste Here (ctrl+v)</b>
  <br>
  <input id="boxx1" type="text" style="width:210px;" onKeyPress="boxx1KeyPress()" onKeyUp="boxx1KeyPress()">
  <br>
  <input type="button" Value="Clear Field" onClick="ClearField()">
  <br>
  <br>
  <b>Output 1</b> <input id="boxx2" type="text" style="width:370px;" readonly> <b>Comp 1</b><input id="comp1" type="text" style="width:180px;" readonly>
  <br>
  <br>
  <b>Output 2</b> <input id="boxx3" type="text" style="width:370px;" readonly> <b>Comp 2</b><input id="comp2" type="text" style="width:180px;" readonly>
</body>

<script>
  function boxx1KeyPress() {
    var boxx1 = document.getElementById("boxx1");
    var s = boxx1.value.replace(/[ ,]+/g, ",");
    var x = s;

    var lblValue = document.getElementById("boxx2");
    lblValue.value = "" + s;

    var points = lblValue.value.split(",");
    document.getElementById("boxx3").value = points.sort().join();


    var comp1 = document.getElementById("boxx2");
    var res = comp1.value.split(",", 4);
    document.getElementById("comp1").value = res;

    var comp2 = document.getElementById("boxx3");
    var res2 = comp2.value.split(",", 4);
    document.getElementById("comp2").value = res2;

  }

  function ClearField() {
    document.getElementById("boxx1").value = "";
    document.getElementById("boxx2").value = "";
    document.getElementById("boxx3").value = "";
    document.getElementById("comp1").value = "";
    document.getElementById("comp2").value = "";

  }
</script>

</html>
&#13;
&#13;
&#13;

嗨,我需要你的帮助。

我迷路了,不知道接下来该做什么。 非常感谢那些可以帮助我的人。

到目前为止我做了什么:

我从一个源代码中得到一组数字:

来自:22 21 65 1 23 54 65 13 11 32 99 65 43 44 55 3 4 5 88 99

产出1结果:22,21,65,1,23,54,65,13,​​11,32,99,65,43,44,55,3,4,5,89,99

Comp1结果:&#34;这就是我需要的&#34;

产出2结果:1,11,13,21,22,23,3,32,4,43,44,5,54,55,65,65,65,88,99,99

Comp2结果:&#34;这就是我需要的&#34;

这是一个20集的数字,只要这些数字贴在&#34;粘贴在这里,就会将空格作为分隔符。文本框 逗号将替换空格并将在输出文本框中显示结果。在第二个输出文本框中,同样的事情将按升序发生。

我想做什么:

从Output1和Output2上显示的数字我需要将这些数字组合成5个,每个数字包含4组数字,并找到每个组的总和如下所示:

22,21,65,1等于10 9

23,54,65,13等于15 5

11,32,99,65等于20 7

43,44,55,3等于14 5

4,5,88,99等于19 6

现在我需要获取每个结果的所有最后数字,并且组合的数字应该显示在Comp 1文本框中,它应该如下所示:

95756

Comp 2文本框上的相同内容基础是输出2文本框。

1 个答案:

答案 0 :(得分:1)

希望这有助于你

&#13;
&#13;
<html>
<title> Test </title>

<body>
  <b>Paste Here (ctrl+v)</b>
  <br>
  <input id="boxx1" type="text" style="width:210px;">
  <br>
  <input type="button" Value="Add" onClick="go()">
  <input type="button" Value="Clear Field" onClick="ClearField()">
  <br>
  <br>
  <b>Output 1</b> <input id="boxx2" type="text" style="width:370px;" readonly> <b>Comp 1</b><input id="comp1" type="text" style="width:180px;" readonly>
  <br>
  <br>
  <b>Output 2</b> <input id="boxx3" type="text" style="width:370px;" readonly> <b>Comp 2</b><input id="comp2" type="text" style="width:180px;" readonly>
</body>
<script>
  function go() {
    var boxx1 = document.getElementById("boxx1");
    var s = boxx1.value.replace(/ /g, ",");

    var lblValue = document.getElementById("boxx2");
    lblValue.value = s;

    var points = s.split(",");

    var comp1 = '';
    for (var i = 0; i < points.length; i += 4) {
      var sum = 0;
      for (var j = i; j < i + 4; j++) {
        sum += Number(points[j]);
      }
      comp1 += (sum % 10);
    }

    document.getElementById("comp1").value = comp1;

    var comp2 = '';
    document.getElementById("boxx3").value = points.sort().join();

    for (var i = 0; i < points.length; i += 4) {
      var sum = 0;
      for (var j = i; j < i + 4; j++) {
        sum += Number(points[j]);
      }
      comp2 += (sum % 10);
    }

    document.getElementById("comp2").value = comp2;
  }

  function ClearField() {
    document.getElementById("boxx1").value = "";
    document.getElementById("boxx2").value = "";
    document.getElementById("boxx3").value = "";
    document.getElementById("comp1").value = "";
    document.getElementById("comp2").value = "";
  }
</script>

</html>
&#13;
&#13;
&#13;