使用 for 循环 totalSum Javascript 将值添加到一个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14735598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add values to one variable with for loop totalSum Javascript
提问by Dymond
I have some values that come randomized to an variable. Now i want to add every value to each other to get an total sum but the loop wont add them together.
我有一些随机到一个变量的值。现在我想将每个值彼此相加以获得总和,但循环不会将它们加在一起。
The index variable work when i console log it. but not the total sum of a current value and the index value. Any tips ?
当我控制台记录它时,索引变量工作。但不是当前值和索引值的总和。有小费吗 ?
This is the loop // The indexValue is values I get from an array//
这是循环 // indexValue 是我从数组中获得的值 //
var indexValue = index; // Random number from 1 - 10
var totalSum = 0;
for(var x = 0; x <indexValue; x++){
var currentValue = index[x];
totalSum += currentValue;
}
console.log(totalSum);
回答by War10ck
I'm assuming since you're referencing index[x]
that index
is an array. If so, you are assigning the wrong value to indexValue
. Try this:
我假设,因为你引用index[x]
这index
是一个数组。如果是这样,则您为 分配了错误的值indexValue
。试试这个:
var indexValue = index.length;
What this does is assign the length of the array to the variable indexValue
. Now the for
loop will run from 0 to n where n is the length of the array.
这样做是将数组的长度分配给变量indexValue
。现在for
循环将从 0 运行到 n,其中 n 是数组的长度。
This should get you the values you need.
这应该可以为您提供所需的值。
Hope this helps.
希望这可以帮助。
EDIT:
编辑:
Below is a link to a jsFiddle I created with your example and the code explained:
以下是我使用您的示例创建的 jsFiddle 的链接,并解释了代码:
var index = [1,2,3,4,5];
var indexValue = index.length; // It's imperative that this is an array
var totalSum = 0;
for(var x = 0; x <indexValue; x++){
totalSum += index[x];
}
alert(totalSum);
The code above is a modified version of what you posted. The current value is not needed and has been removed. In this case, I created an array and stored the values in index. The length is computed, and the values are added together.
上面的代码是您发布的内容的修改版本。当前值不需要并且已被删除。在这种情况下,我创建了一个数组并将值存储在索引中。计算长度,并将值相加。
For the easy sake of testing, I changed console.log(totalSum);
to alert(totalSum);
. It will produce the same value just the same.
对于为了方便测试,我换console.log(totalSum);
到alert(totalSum);
。它将产生相同的值。
Just a reference note on console.log
. Some browsers (mainly IE) who do not attach the debugger to the process automatically without a page refresh will through an error as console
will be undefined
. Here is a quick fix for that:
只是一个参考注释console.log
。有些浏览器(IE为主),谁不调试器不需要刷新页面自动连接到进程将通过一个错误console
会undefined
。这是一个快速解决方案:
if(window.console && window.console.log) {
console.log(totalSum);
}
This checks that (a) the console object is attached to the global window object and (b) that the log object of console is attached to the console object.
这会检查 (a) 控制台对象是否附加到全局窗口对象,以及 (b) 控制台的日志对象是否已附加到控制台对象。
回答by Mike Corcoran
you probably want something like this:
你可能想要这样的东西:
var sum = 0;
for (var i=0; i < index.length; i++) {
sum += index[i];
}
console.log(sum);