Reputation: 1
I have an array like this c=[1,2,3,4,5,6,7,8,9,10]
if I want to execute the sum of each alternating two elements, like 1+2, then 5+6, then 9+10, how the code should be written? I can do the sum of each 2 elements.
Upvotes: 0
Views: 19
Reputation: 304
You could simply use a for loop do so.
var c=[1,2,3,4,5,6,7,8,9,10]
var sum = [];
for (var i = 0; i < c.length; i += 2)
sum.push(c[i] + c[i+1])
for (var j = 0; j < sum.length; j++)
console.log(sum[j]);
Upvotes: 1