Code to find the summation of N numbers using Reduce Function of JavaScript

Rajnilari2015
Posted by Rajnilari2015 under JavaScript category on | Points: 40 | Views : 371
In the below code, we will find an example that does the summation of N numbers using Reduce Function of JavaScript

var sum = 0;  
sum = [16,23,3,49,55,689,7,8,912,10].reduce(function(a,b){return a + b})
console.log("The sum is: " + sum);


So, in the first attempt, 16 and 23 were passed and the reduce function acted on it to bring the result/reduce the result to 39. In the next attempt, initial value was 39 (from original array) and next value was 3 from the previous computation which resulted in 42 and like that.

For n array elements, the Reduce function is called n-1 times.

Comments or Responses

Login to post response