Last active
November 2, 2019 10:55
-
-
Save sri-teja/3989a8f3ebcc49b093e407e284664768 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Sri Teja <[email protected]> | |
* @link sri-teja.github.io | |
*/ | |
// Syntax: | |
// <array>.reduce(<function>, start_value); | |
// The reduce () method reduces the array to a single value. | |
// The reduce() method executes a provided function for each value | |
// of the array (from left-to-right). | |
// The return value of the function is stored in an accumulator (result/total). | |
// Example1: | |
function multiply(a,b){ return a*b; } | |
result = [2,3,4].reduce(multiply, 1); //result gives 24 = 2*3*4. | |
// Let's see how this works in detail | |
// step1: a=1 and b=2; return value from multiply() is 1*2 = 2; | |
// step2: a=2(the previous return value from the multiply()) and b=3; | |
// return value from mutiply() is 2*3 = 6; | |
// step3: a=6 and b=4; return value from multiply() is 6*4 = 24; | |
// Another Example given below | |
// Example2: | |
function sum(a,b){ return a+b; } | |
result = [1,2,3].reduce(sum, 0); //result gives 6 = 1+2+3. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment