I need to sum all my numbers from a for loop with javascript
var nums = ['100','300','400','60','40'];
for(var i=1; i < nums.length; i++){
var num = nums[i] + nums[i];
alert(num);
}
can someone help http://jsfiddle.net/GYpd2/1/
the outcome i am looking for is 900
14 Answers 14
var nums = ['100','300','400','60','40'];
var sum = 0;
for(var i=0; i < nums.length; i++){
sum += parseInt(nums[i]);
}
alert(sum);
Tested: http://jsfiddle.net/GYpd2/6/ (thanks to user1503606)
If nums contains numbers only there is no need for parseInt().
Comments
Prime example for ES5's Array.prototype.reduce method. Like:
var nums = ['100','300','400','60','40'];
var total = nums.reduce(function(a,b) {
return (+a)+(+b);
});
Comments
var nums = ['100','300','400','60','40'],
num = 0;
for (var i = 0; i < nums.length; i++) {
num += +nums[i];
}
alert(num);
3 Comments
+ on the left of nums[i]; ?Do it like this
var nums = ['100','300','400','60','40'];
var total = 0;
for(var i=0; i < nums.length; i++){
total = total + Number(nums[i]);
}
alert(total);
The loop starts with 0 not 1.
totalvariable needs to be declared before the loop or else it will not preserve the previous addition.Use Number() to convert string to number.
Adding string means concatitation
'100'+'200'will give'100200'.
Comments
var num, nums = [100,300,400,60,40];
for ( var i=1; i < nums.length; i++ ) {
num += nums[i];
}
alert(num);
Comments
If you have a reduce function , you can just do this:
var nums = ['100','300','400','60','40'],
sum = nums.reduce(function(accum, val) {return accum + Number(val);}, 0);
alert(sum);
Comments
var nums = ['100','300','400','60','40'];
var num = 0;
for(var i=0; i < nums.length; i++){
num = parseInt(num) + parseInt(nums[i]);
}
This is how I did it. It's very similar to other's code, but a different way of writing it. You do need to start your initial i value in your for loop with 0.
1 Comment
Here is JSFiddle
and code is:
var num=0, nums = ['100','300','400','60','40'];
for(var i=0; i < nums.length; i++){
num += parseInt(nums[i]);
}
alert(num);
Comments
Javascript is treating your numbers as strings, and concatenating them together instead of adding them like you expect.
Use parseInt to convert them into integers before adding:
var nums = ['100','300','400','60', 40];
var num = 0;
for(var i=1; i < nums.length; i++){
num += parseInt(nums[i], 10);
alert(num);
}
The second parameter tells parseInt to use base 10.
2 Comments
varvar i, sum = 0, nums = ['100','300','400','60','40'];
for (i = 0; i < nums.length; i++) {
sum += +nums[i];
}
alet(sum);
You shouldn't use the var statement inside a loop. Also the parseInt function used in other answers will always convert the number to an integer so it wouldn't work with floating point numbers. Prefixing the numbers with + will convert them to a number.
If you develop for the browser, using the reduce function might cause problems with older browsers - unless you find a polyfill for it.
Comments
Here's an example that uses an extension of the Number prototype, similar to LINQ's sum method:
Array.prototype.sum = function () {
var result = 0;
this.forEach(function (o) { result += Number(o); });
return result;
};
var sum = ['100','300','400','60','40'].sum();
Comments
You can do something like this..
var nums = ['100','300','400','60','40'];
nums.reduceRight(function(a,b){return Number(a)+Number(b);})
Comments
var nums = ['100','300','400','60','40'];
let sum =0;
for(let numbers of nums){
sum+=parseInt(numbers);
}
alert(sum);
2 Comments
You can try this:
var arr = ['100','300','400','60','40'];
var total = 0;
for(var i in arr){
total += parseInt(arr[i]);
}
console.log(total);
Output will be: 900
Or if value is Float, then try this:
var arr = [100.00,300.50,400.75,60.00,40.00];
var total = 0;
for(var i in arr){
total += parseFloat(arr[i]);
}
console.log(total.toFixed(2));
Output will be: 901.25
num. I recommend to read the MDN JavaScript Guide to learn the basics.