Last active
June 21, 2018 07:01
-
-
Save mirajehossain/c24eed5e33d693a8c7f11749c320f3f3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let arr = [1,2,3,4,5]; | |
/// with initial value | |
let result = arr.reduce((accumulator,currentValue,currentIndex,array)=>{ | |
console.log(`result: ${accumulator}, current value: ${currentValue}, index: ${currentIndex}, array: ${array} `); | |
return accumulator + currentValue; | |
},0); | |
///OUTUT: | |
result: 0, current value: 1, index: 0, array: 1,2,3,4,5 | |
result: 1, current value: 2, index: 1, array: 1,2,3,4,5 | |
result: 3, current value: 3, index: 2, array: 1,2,3,4,5 | |
result: 6, current value: 4, index: 3, array: 1,2,3,4,5 | |
result: 10, current value: 5, index: 4, array: 1,2,3,4,5 | |
/// without initial value | |
let result = arr.reduce((accumulator,currentValue,currentIndex,array)=>{ | |
console.log(`result: ${accumulator}, current value: ${currentValue}, index: ${currentIndex}, array: ${array} `); | |
return accumulator + currentValue; | |
}); | |
/// OUTPUT: | |
result: 1, current value: 2, index: 1, array: 1,2,3,4,5 | |
result: 3, current value: 3, index: 2, array: 1,2,3,4,5 | |
result: 6, current value: 4, index: 3, array: 1,2,3,4,5 | |
result: 10, current value: 5, index: 4, array: 1,2,3,4,5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment