给定一个数组[1, 2, 3, 4]
,如何找到其元素的总和?(在这种情况下,总和为10
。)
我认为$.each
可能有用,但是我不确定如何实现它。
给定一个数组[1, 2, 3, 4]
,如何找到其元素的总和?(在这种情况下,总和为10
。)
我认为$.each
可能有用,但是我不确定如何实现它。
This is much easier
function sumArray(arr) {
var total = 0;
arr.forEach(function(element){
total += element;
})
return total;
}
var sum = sumArray([1,2,3,4])
console.log(sum)
Those are really great answers, but just in case if the numbers are in sequence like in the question ( 1,2,3,4) you can easily do that by applying the formula (n*(n+1))/2 where n is the last number
No need to initial value
! Because if no initial value
is passed, the callback function
is not invoked on the first element of the list, and the first element is instead passed as the initial value
. Very cOOl feature :)
[1, 2, 3, 4].reduce((a, x) => a + x) // 10
[1, 2, 3, 4].reduce((a, x) => a * x) // 24
[1, 2, 3, 4].reduce((a, x) => Math.max(a, x)) // 4
[1, 2, 3, 4].reduce((a, x) => Math.min(a, x)) // 1
You can combine reduce() method with lambda expression:
[1, 2, 3, 4].reduce((accumulator, currentValue) => accumulator + currentValue);
Use a for
loop:
const array = [1, 2, 3, 4];
let result = 0;
for (let i = 0; i < array.length - 1; i++) {
result += array[i];
}
console.log(sum); // Should give 10
Or even a forEach
loop:
const array = [1, 2, 3, 4];
let result = 0;
array.forEach(number => {
result += number;
})
console.log(result); // Should give 10
For simplicity:
const array = [10, 20, 30, 40];
const add = (a, b) => a + b
const result = array.reduce(add);
console.log(result); // Should give 100
Use reduce
let arr = [1, 2, 3, 4];
let sum = arr.reduce((v, i) => (v + i));
console.log(sum);
A short piece of JavaScript code would do this job:
var numbers = [1,2,3,4];
var totalAmount = 0;
for (var x = 0; x < numbers.length; x++) {
totalAmount += numbers[x];
}
console.log(totalAmount); //10 (1+2+3+4)
A standard JavaScript solution:
var addition = [];
addition.push(2);
addition.push(3);
var total = 0;
for (var i = 0; i < addition.length; i++)
{
total += addition[i];
}
alert(total); // Just to output an example
/* console.log(total); // Just to output an example with Firebug */
This works for me (the result should be 5). I hope there is no hidden disadvantage in this kind of solution.
Funny approach:
eval([1,2,3].join("+"))
Anyone looking for a functional oneliner like me? Take this:
sum= arr.reduce(function (a, b) {return a + b;}, 0);
You can also use reduceRight.
[1,2,3,4,5,6].reduceRight(function(a,b){return a+b;})
which results output as 21.
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight
arr.reduce(function (a, b) {
return a + b;
});
Reference: Array.prototype.reduce()
var arr = [1,2,3,4];
var total=0;
for(var i in arr) { total += arr[i]; }
为什么不减少?通常,这有点反直观,但使用它来查找总和非常简单:
var a = [1,2,3];
var sum = a.reduce(function(a, b) { return a + b; }, 0);
var total = 0;
$.each(arr,function() {
total += this;
});
在Lisp中,这正是要完成的工作reduce
。您会看到以下代码:
(reduce #'+ '(1 2 3)) ; 6
幸运的是,在JavaScript中,我们还有reduce
!不幸的是,它+
是一个运算符,而不是一个函数。但是我们可以使其漂亮!在这里,看看:
const sum = [1, 2, 3].reduce(add,0); // with initial value to avoid when the array is empty
function add(accumulator, a) {
return accumulator + a;
}
console.log(sum); // 6
那不是很漂亮吗?:-)
更好!如果您使用的是ECMAScript 2015(又名ECMAScript 6),它可能会很漂亮:
const sum = [1, 2, 3].reduce((partial_sum, a) => partial_sum + a,0);
console.log(sum); // 6
If you happen to be using Lodash you can use the sum function
array = [1, 2, 3, 4];
sum = _.sum(array); // sum == 10
A simple method example: