英文:
How to clear an array after each call?
问题
let sum = 0;
var runningSum = function (nums2) {
let result = []
while (result.length > 0) {
result.pop();
}
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum)
}
return result
};
你的代码中有两处错误:
- 在
while
循环条件中,应该是result.length > 0
,而不是result.length > 0
。 - 在第二次调用
runningSum([1,1,1])
时,你期望的输出是[1, 2, 3]
,但实际上因为sum
没有被重置为 0,所以结果是[4, 5, 6]
。
你可以将 sum
的初始值和每次调用 runningSum
前重置 sum
的操作移到函数内部,以确保每次调用时 sum
都是初始值 0。
英文:
let sum = 0;
var runningSum = function (nums2) {
let result = []
while (result.length > 0) {
result.pop();
}
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum)
}
return result
};
I get the following output:
runningSum([1,1,1]) //=> [1, 2, 3]
runningSum([1,1,1]) //=> [4, 5, 6]
I'm expecting the following output:
runningSum([1,1,1]) //=> [1, 2, 3]
runningSum([1,1,1]) //=> [1, 2, 3]
What am I doing wrong?
答案1
得分: 3
问题在于每次调用 runningSum
函数时,变量 sum
并不会被重置为零,因为它是在 runningSum
函数外部定义的。这意味着 sum
会保留上一次调用的值,并继续累加到当前的总和。
var runningSum = function(nums2) {
let sum = 0;
let result = [];
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum);
}
return result;
};
console.log(runningSum([1, 1, 1]));
console.log(runningSum([1, 1, 1]));
英文:
The problem is that each time runningSum is called, the variable sum is not reset to zero because it is defined outside of the runningSum function. This indicates that sum keeps the value from the prior call and keeps adding to the ongoing sum.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var runningSum = function(nums2) {
let sum = 0;
let result = [];
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum);
}
return result;
};
console.log(runningSum([1, 1, 1]));
console.log(runningSum([1, 1, 1]));
<!-- end snippet -->
答案2
得分: 0
根据评论指出,您需要在函数内部本地定义变量 sum
,并可以摆脱 while
循环。
通过在函数内部定义 sum
,每次调用函数时都会创建一个新的变量。
如果您恰好需要在函数外部使用名为 sum
的变量,仍然可以这样做。尽管它们具有相同的名称,但它们是完全不同的变量,互不影响:
let sum = 0;
var runningSum = function (nums2) {
let sum = 0;
let result = []
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum)
}
return result
};
console.log(sum)
console.log(runningSum([1,1,1]))
sum = 123456789
console.log(sum)
console.log(runningSum([1,1,1]))
英文:
As the comment point out, you need to define the variable sum
locally in your function and you can get rid of the while.
By defining sum
within your function, then it will be created new every time the function is called.
If you happen to need a variable named sum
outside the function, you can still do so. Despite having the same name, they are completely different variable that do not affect one another:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let sum = 0;
var runningSum = function (nums2) {
let sum = 0;
let result = []
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum)
}
return result
};
console.log(sum)
console.log(runningSum([1,1,1]))
sum = 123456789
console.log(sum)
console.log(runningSum([1,1,1]))
<!-- end snippet -->
答案3
得分: 0
你可以将代码简化为:
function runningSum(nums2) {
let sum = 0;
return nums2.map(x => {
sum += x;
return sum;
})
}
const arr1 = [1, 1, 1];
const sum1 = runningSum(arr1);
console.log(sum1)
const arr2 = [1, 1, 1];
const sum2 = runningSum(arr2);
console.log(sum2)
英文:
You can simplify your code as it:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function runningSum(nums2) {
let sum = 0;
return nums2.map(x => {
sum += x;
return sum;
})
}
const arr1 = [1, 1, 1];
const sum1 = runningSum(arr1);
console.log(sum1)
const arr2 = [1, 1, 1];
const sum2 = runningSum(arr2);
console.log(sum2)
<!-- end snippet -->
答案4
得分: 0
很好,其他人简化你的代码(最终应该这样做),但这个答案将向你展示为什么每次调用函数时 sum 不会重置。由于 sum 在函数外部设定,其值不会被重置。我将其移至函数内部。
var runningSum = function (nums2) {
let sum = 0;
let result = []
while (result.length > 0) {
result.pop();
}
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum)
}
return result;
};
英文:
It's great that others are simplifying your code (which should be done eventually), but this answer will show you why sum is not reset upon each call to the function. Since sum is set outside the function, the value is not reset. I moved it inside the function.
var runningSum = function (nums2) {
let sum = 0;
let result = []
while (result.length > 0) {
result.pop();
}
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum)
}
return result;
};
答案5
得分: 0
问题在于,你在函数外部声明了你的 sum
变量并且从未将其重置。所以在你第二次调用 runningSum
时,你实际上已经从 sum = 3
开始,而不是 sum = 0
。
但实际上,你根本不需要那个变量。对于 result[i]
的值,只需将结果数组中的前一个元素和输入数组中的当前元素相加即可。result[i] = result[i-1] + arr[i]
。对于索引 i = 0
,你必须采取预防措施并添加一个回退到 0
,因为 result[-1]
会返回 undefined
。
function runningSum(arr) {
let result = [];
for (let i = 0; i < arr.length; i++)
result[i] = (result[i-1] || 0) + arr[i];
return result;
}
console.log(runningSum([]));
console.log(runningSum([1]));
console.log(runningSum([1,1,1]));
console.log(runningSum([1,2,3]));
英文:
The problem is, you are declaring your sum
outside of your function and never resetting it. So in your second call of runningSum
you are already starting with a sum = 3
instead of sum = 0
But actually, you don't need that variable at all. For the value of result[i]
just add the previous element in the result array and the current element in the input array. result[i] = result[i-1] + arr[i]
. For index i = 0
you have to take precautions and add a fallback to 0
, because result[-1]
will give undefined
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function runningSum(arr) {
let result = [];
for (let i = 0; i < arr.length; i++)
result[i] = (result[i-1] || 0) + arr[i];
return result;
}
console.log(runningSum([]));
console.log(runningSum([1]));
console.log(runningSum([1,1,1]));
console.log(runningSum([1,2,3]));
<!-- end snippet -->
答案6
得分: 0
以下是翻译好的部分:
// 开始代码段
var runningSum = function (nums2) {
let result = []
// 可以在这里编写 sum
let sum = 0;
// 这个部分可以移除,没有任何作用
// while (result.length > 0) {
// result.pop();
// }
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum)
}
return result
};
// 这是用于测试的
console.log(runningSum([1,1,1]))
console.log(runningSum([1,1,1]))
// 结束代码段
英文:
just write something like that (let sum=0 goes inside function)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var runningSum = function (nums2) {
let result = []
// u can write sum here
let sum = 0;
// this is u can remove this does nothing
// while (result.length > 0) {
// result.pop();
// }
for (let i = 0; i < nums2.length; i++) {
sum += nums2[i];
result.push(sum)
}
return result
};
// this is for test
console.log(runningSum([1,1,1]))
console.log(runningSum([1,1,1]))
<!-- end snippet -->
答案7
得分: 0
The sum
variable should not exist globally (but within the function), otherwise it will never be reset to 0.
Maybe you can simplify your function using a reducer
, something like:
const runningSum = (...values) => values
.reduce( (acc, value) => acc.concat(value + (acc[acc.length-1] || 0)), []);
console.log(`1, 1, 1 => ${JSON.stringify(runningSum(1,1,1))}`);
const someValues = [1,3,5,7,9];
console.log(`${someValues.join(', ')} => ${
JSON.stringify(runningSum(...someValues))}`);
英文:
The sum
variable should not exist globally (but within the function), otherwise it will never be reset to 0.
Maybe you can simplify your function using a reducer
, something like:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const runningSum = (...values) => values
.reduce( (acc, value) => acc.concat(value + (acc[acc.length-1] || 0)), []);
console.log(`1, 1, 1 => ${JSON.stringify(runningSum(1,1,1))}`);
const someValues = [1,3,5,7,9];
console.log(`${someValues.join(`, `)} => ${
JSON.stringify(runningSum(...someValues))}`);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论