从矩阵的行中创建一个正元素和数组。

huangapple go评论65阅读模式
英文:

Create an array from sum of positive elements from rows in matrix

问题

Sure, here's the translated portion:

我想将一行中的正数之和制成一个数组,但是我的代码如果负数位于第一列,它会计算负元素。请问我能得到一些帮助吗?

我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
</head>
<body>
    <div id="result-container"></div>
    <script>
    let myArray = [3]; 

        for (let i = 0; i < 3; i++) { 
            myArray[i] = [];  
            for (let j = 0; j < 3; j++) { 
                myArray[i][j] = Math.floor(Math.random() * 20 - 10) + 1;  
            } 
        } 

    let output = myArray;

    console.log(myArray);
    let F = [];

        for (let i = 0; i < myArray[0].length; i++) {
            let posSum = myArray[i][0];
            for (let j = 1; j < myArray.length; j++) {
                if (myArray[i][j] > 0) {
                    posSum += myArray[i][j];
                }
            }
            F.push(posSum);
        }
        output += `Array F: ${F}`;
        console.log(F);
        document.getElementById('result-container').innerHTML = output;
    </script>
</body>
</html>

问题示例:
问题示例

英文:

I want to make an array out of sum of positive numbers in row, but my code counts negative elements if they're standing in first column. Can I get some help with this please?

My code:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div id=&quot;result-container&quot;&gt;&lt;/div&gt;
          &lt;script&gt;
          let myArray = [3]; 
        
              for (let i = 0; i &lt; 3; i++) { 
                myArray[i] = [];  
                for (let j = 0; j &lt; 3; j++) { 
                  myArray[i][j] = Math.floor(Math.random() * 20 - 10) + 1;  
                } 
        } 
          
        let output = myArray;

        
        console.log(myArray);
        let F = [];

              for (let i = 0; i &lt; myArray[0].length; i++) {
                let posSum = myArray[i][0];
                for (let j = 1; j &lt; myArray.length; j++) {
                  if (myArray[i][j] &gt; 0) {
                    posSum += myArray[i][j];
                  }

                }
          F.push(posSum);
        }
        output += `Array F: ${F}`;
        console.log(F);
        document.getElementById(&#39;result-container&#39;).innerHTML = output;
          &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Example of problem:
problem

答案1

得分: 0

The culprit is this line of code:

这个问题出现在以下这行代码:

let maxElement = myArray[i][0];

Here you're intentionally setting your counter to the value of the first element inside the array. So if it's e.g. -1 you're starting to count from -1 and not zero.

在这里,你故意将你的计数器设置为数组中第一个元素的值。所以,如果它是 -1,你就从 -1 开始计数,而不是零。

Also the second for-loop increments from element 1 onwards - it should start at index 0.

此外,第二个for循环从第1个元素递增,它应该从索引0开始。

Let me know if you need any further assistance.

如果需要进一步的帮助,请告诉我。

英文:

The culprit is this line of code:

let maxElement = myArray[i][0];

Here you're intentionally setting your counter to the value of the first element inside the array. So if it's e.g. -1 you're starting to count from -1 and not zero.

Also the second for-loop increments from element 1 onwards - it should start at index 0.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let myArray = [3];

for (let i = 0; i &lt; 3; i++) {
  myArray[i] = [];
  for (let j = 0; j &lt; 3; j++) {
    myArray[i][j] = Math.floor(Math.random() * 20 - 10) + 1;
  }
}

let output = myArray;


console.log(myArray);
let F = [];

for (let i = 0; i &lt; myArray[0].length; i++) {
  let maxElement = 0;
  for (let j = 0; j &lt; myArray.length; j++) {
    if (myArray[i][j] &gt; 0) {
      maxElement += myArray[i][j];
    }

  }
  F.push(maxElement);
}
console.log(F);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 20:00:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定