分隔JavaScript正数和负数

t98cgbkg  于 2022-10-22  发布在  Java
关注(0)|答案(2)|浏览(196)

有人能帮我找出错误吗?我的主要目标是,当我们输入一个数组时,得到的结果与下面所示的相同,Input

{-5,-4,10,12,13,4,5,-8,-6}

输出

{10,12,13,4,5,-5,-4,-8,6}

我在这里附上了我的完整代码。我没有输出。enter image description here

<!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <!--
        Positive Numbers one side and negetive numbers other side
      -->
      <title>RK1</title>
    </head>
    <body>
      <H3>RK1</H3>
      <form.submit()>
        <p>Enter Array</p>
        <input type="text" id="text" placeholder="Enter Text"><br>
        <p>Changed array</p>
        <p id="text2"></p>
        <p>plus Array</p>
        <p id="text3"></p>
        <button onclick="foo()">Give Answer</button>
      </form>
      <script type="text/javascript">
          function foo(){
            var arrch= document.getElementById("text").value.spilt(" ").map(Number);
            var dlina=arrch.length;
            var new_arr =[];
            var new_arr2=[];
            var k=0;
            var summa=0;
            var n=0;

            for(var i=0;i<dlina;i++){
              if(arrch[i]>0){
                summa+=arrch[i];
                new_arr[n]=arrch[i];
                n++
              }
              else if(arrch[i]<0){
                new_arr2[k]=arrch[i];
                k++

              }
            }
            new_arr = new_arr.join(" ")
            new_arr2 = new_arr2.join(" ")

              document.getElementById("text2").innerHTML = new_arr + " " +new_arr2;
              document.getElementById("text3").innerHTML = summa;
          }
      </script>

    </body>
    </html>
kcwpcxri

kcwpcxri1#

如果只想将负数排序到数组的末尾,可以根据数字的符号进行sort。您可以使用reduce对数组求和:

const arr = [-5,-4,10,12,13,4,5,-8,-6]

arr.sort((a, b) => Math.sign(b) - Math.sign(a))

console.log(arr)

const sum = arr.reduce((acc, el) => acc + el)

console.log(sum)

下面是您的代码的修改版本,其中包含了这些更改(@EricFortis更正了split中的错误):

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!--
        Positive Numbers one side and negetive numbers other side
      -->
  <title>RK1</title>
</head>

<body>
  <H3>RK1</H3>
  <form>
    <p>Enter Array</p>
    <input type="text" id="text" placeholder="Enter Text"><br>
    <p>Changed array</p>
    <p id="text2"></p>
    <p>plus Array</p>
    <p id="text3"></p>
    <button onclick="foo(event)">Give Answer</button>
    </form>
    <script type="text/javascript">
      function foo(e) {
        e.preventDefault();
        var arrch = document.getElementById("text").value.split(" ").map(Number);
        arrch.sort((a, b) => Math.sign(b) - Math.sign(a))
        var summa = arrch.reduce((acc, el) => acc + el)

        document.getElementById("text2").innerText = arrch;
        document.getElementById("text3").innerText = summa;
      }
    </script>

</body>

</html>
bpsygsoo

bpsygsoo2#

你可以这样编码。。。

const myForm = document.querySelector('#my-form')

myForm.onsubmit = e =>
  {
  e.preventDefault() 
  let res = myForm.txtNums.value
    .match(/([-+]?\d+)/g).map(Number)
    .reduce((r,n) =>
      {
      r[(n<0?'neg':'pos')].push(n)
      r.sum += n
      return r 
      }
      ,{sum:0,pos:[],neg:[]}
    ) 
  myForm.txtOut_1.textContent = `{${res.pos.concat(res.neg).join(',')}}`
  myForm.txtOut_2.textContent = res.sum
  }
body {
  font-family : Arial, Helvetica, sans-serif;
  font-size   : 16px;
}
label {
  display   : block;
  font-size : .8rem;
  padding   : .4rem .2rem;
  }
input,
output {
  display   : block;
  border    : 1px solid lightslategrey;
  width     : 16rem;
  font-size : 1rem;
  padding   : .2rem .4em;
  }

a

<form id="my-form">
  <label>Enter Array
    <input type="text" name="txtNums"
           placeholder="Enter Text"
           value="{-5,-4,10,12,13,4,5,-8,-6}">
  </label>  
  <label>Changed array 
    <output name="txtOut_1">.</output>
  </label> 
  <label>plus Array
    <output name="txtOut_2">.</output>
  </label> 
  <button>Give Answer</button>
</form>

相关问题