Skip to content

Instantly share code, notes, and snippets.

@fakefarm
Created August 26, 2014 15:04
Show Gist options
  • Save fakefarm/6a978ba9c09becf2730f to your computer and use it in GitHub Desktop.
Save fakefarm/6a978ba9c09becf2730f to your computer and use it in GitHub Desktop.
reduce
function reduce(array, start, callback) {
  // javascript, and all of programming often needs variables to contain state.
  var current = start, 
      freq = array.length; 
      

  for (var i = 0; i < freq; i++){
    // how much of learning is problem solving vs. syntax?
    current = callback(current, array[i]); 
  }
  // as a function runs, it will need to return to it's transitionary value
  return current; 
}

console.log(reduce(
             [1, 2, 3, 14], 
             0, 
                   function(a, b) { return (a * 2) + b;}
                  )
           );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment