Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Lelana villa
Added: Mar 23, 2018 7:48 PM
Views: 14
Tags: no tags
  1. var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  2.  
  3. var total = numbers.reduce(function(sum, value) {
  4. return sum + value;
  5. }, 0);
  6.  
  7. console.log(total);
  8.  
  9.  
  10. // here I don't provide the seconds argument (initial value) ->
  11. // at first call, sum = numbers[0] and value = numbers[1] so the result is the same
  12. var total = numbers.reduce(function(sum, value) {
  13. return sum + value;
  14. });
  15.  
  16. console.log(total);
  17.  
  18.  
  19. var concatenate = numbers.reduceRight((str, value) => str = str + value, '');
  20. console.log(concatenate); // '987654321'
  21.  
  22. // we start from '' add concatenate each value, starting from the right