Javascript 在for循环javascript中添加数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11686724/
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
adding numbers in for loop javascript
提问by user1503606
I need to sum all my numbers from a for loop with javascript
我需要用 javascript 从 for 循环中总结我所有的数字
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/
有人可以帮忙http://jsfiddle.net/GYpd2/1/
the outcome i am looking for is 900
我要找的结果是 900
回答by Besnik
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)
测试:http: //jsfiddle.net/GYpd2/6/(感谢user1503606)
If nums
contains numbers only there is no need for parseInt()
.
如果nums
只包含数字,则不需要parseInt()
.
回答by jAndy
Prime example for ES5's Array.prototype.reduce
method. Like:
ES5Array.prototype.reduce
方法的主要示例。喜欢:
var nums = ['100','300','400','60','40'];
var total = nums.reduce(function(a,b) {
return (+a)+(+b);
});
回答by Vitalii Petrychuk
var nums = ['100','300','400','60','40'],
num = 0;
for (var i = 0; i < nums.length; i++) {
num += +nums[i];
}
alert(num);
?
回答by aefxx
var num, nums = [100,300,400,60,40];
for ( var i=1; i < nums.length; i++ ) {
num += nums[i];
}?
alert(num);
回答by Subir Kumar Sao
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.
total
variable 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'
.
循环从 0 开始,而不是 1。
total
变量需要在循环之前声明,否则它不会保留之前的添加。使用 Number() 将字符串转换为数字。
添加字符串意味着串联
'100'
+'200'
将给出'100200'
.
回答by Scott Sauyet
If you have a reduce function , you can just do this:
如果你有一个reduce 函数,你可以这样做:
var nums = ['100','300','400','60','40'],
sum = nums.reduce(function(accum, val) {return accum + Number(val);}, 0);
alert(sum);
回答by Max Kueng
var 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.
您不应该var
在循环内使用该语句。此外parseInt
,其他答案中使用的函数将始终将数字转换为整数,因此它不适用于浮点数。将数字作为前缀+
会将它们转换为数字。
If you develop for the browser, using the reduce
function might cause problems with older browsers - unless you find a polyfill for it.
如果您为浏览器开发,使用该reduce
功能可能会导致旧浏览器出现问题 - 除非您为它找到了 polyfill。
回答by user201827
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.
我就是这样做的。它与其他人的代码非常相似,但编写方式不同。您确实需要在 for 循环中以 0 开始初始 i 值。
回答by Matthew Layton
Here's an example that uses an extension of the Number prototype, similar to LINQ's sum method:
这是一个使用 Number 原型扩展的示例,类似于 LINQ 的 sum 方法:
Array.prototype.sum = function () {
var result = 0;
this.forEach(function (o) { result += Number(o); });
return result;
};
var sum = ['100','300','400','60','40'].sum();
回答by SuperNova
You can do something like this..
你可以做这样的事情..
var nums = ['100','300','400','60','40'];
nums.reduceRight(function(a,b){return Number(a)+Number(b);})