admin管理员组文章数量:1400115
I have some values that e 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//
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);
I have some values that e 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//
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);
Share
Improve this question
asked Feb 6, 2013 at 17:52
DymondDymond
2,2877 gold badges46 silver badges82 bronze badges
4
-
1
I think it should be
var indexValue = index.length;
– Pointy Commented Feb 6, 2013 at 17:53 -
What is
index
? What exactly does it mean if a variable "does not work"? – Felix Kling Commented Feb 6, 2013 at 17:53 - 1 on your first line, were u intending to do "var indexValue = index.length" ? – Dogoku Commented Feb 6, 2013 at 17:56
- have tried with index.length. But no luck – Dymond Commented Feb 6, 2013 at 18:04
2 Answers
Reset to default 2I'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:
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.
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 puted, 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.
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:
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.
you probably want something like this:
var sum = 0;
for (var i=0; i < index.length; i++) {
sum += index[i];
}
console.log(sum);
本文标签: add values to one variable with for loop totalSum JavascriptStack Overflow
版权声明:本文标题:add values to one variable with for loop totalSum Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744232508a2596405.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论