英文:
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:
<!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>
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 < 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 maxElement = 0;
for (let j = 0; j < myArray.length; j++) {
if (myArray[i][j] > 0) {
maxElement += myArray[i][j];
}
}
F.push(maxElement);
}
console.log(F);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论