Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  25] [ 4]  / answers: 1 / hits: 145616  / 12 Years ago, fri, july 27, 2012, 12:00:00

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


More From » javascript

 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().


[#84024] Wednesday, July 25, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
terrence questions
Sat, Jun 5, 21, 00:00, 3 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
;