Hacker News new | past | comments | ask | show | jobs | submit | jcrowe206's comments login

fourth solution would be naming the function with the iterator (as in my example below) and calling indexOf(iterator) when the function is invoked


Here's another one. Probably not very performant

  function foo(count) {
     var ret = []
     for (var i=0; i < count; i++) {
        ret [i] = function i() { return this.indexOf(i) }
     }
     return ret;
  }


Just fyi indexOf is O(n), which means if you tried to do:

  var count = 1000000;
  var arr = foo(count);
  for (var i = 0; i < count; i++)
    arr[i]();
It will take O(n^2) and will probably never complete whereas all the other solutions here will probably take ~1second.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: