@cmarmar/

Reduce Right

ES6

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Create a function called reduceRight that accepts an array, a starting value and an iterating function.  Instead of iterating over the array from left to right, have the reduceRight function start from the last element in the array.

function reduceRight(arr, accumulator, cb){
  var result=accumulator;
  var arrCopy = arr;
  for ( var i = arrCopy.length-1; i>=0; i--) {
    if(typeof accumulator=== 'object'){
      result = cb(accumulator, arrCopy[i])
    }
    else{
      result+= cb(accumulator, arrCopy[i])
    }
  }
  return result
}




function reduceRightRecursive( arr, accumulator, cb){
  var result=""
  if(arr.length===0){
    //base case
    result+= cb(accumulator, arr[0])
  }
  else{
    result+= cb(reduceRightRecursive(result), arr.slice(0, arr.length))
  }
  return result
}
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie