node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



Repeatedly apply a function.

iterate(limit, inital_value, func)
  // or: 
iterate(limit, func)

func gets passed two arguments:

iterate(limit, inital_value, func(index, applied_value) { do_stuff(); })

'index' is the current index of the loop. 'applied_value' is the return value of that function for the previous iteration.

Simple example:

  iterate(3, function(i) { console.log(i) })
  > 0
  > 1
  > 2

Using the applied value to get the powers of 2:

power = iterate(4, 1, function(i, applied) { return applied * 2 });
> 16

see similar: