Live data from Hacker News

Front End Developer – Interview Questions

github.com

151–152 of 152 posts

Re: Front End Developer – Interview Questions

#151
post #74

Earlier quoted context omitted.

You could use Function.prototype.bind in a couple ways, or even Array.prototype.map (though sadly you can't really do new Array(n).map, if that was true you could make this a one-liner). // new Array(n) could just be [] in all examples // // using Array.prototype.map // sadly you can't do new Array(n).map // because it doesn't iterate over undefined functions function foo(n) { for (var ret = new Array(n), i = 0; i

If you're into one-liners, CoffeeScript is your friend :) foo = (n) -> (-> i) for i in [0..n]

Close, but I checked it and the value of `i` won't bind to the function. You also have to use `...` (exclusive range) instead of `..` to prevent an off-by-one error.

Here's one way:

    foo = (n) -> ((a) -> a).bind(null, i) for i in [0...n]
Here's a more esoteric/bad style (but cute) way:

    foo = (n) -> (-> +@).bind i for i in [0...n]

Re: Front End Developer – Interview Questions

#152

Earlier quoted context omitted.

If you're into one-liners, CoffeeScript is your friend :) foo = (n) -> (-> i) for i in [0..n]

Close, but I checked it and the value of `i` won't bind to the function. You also have to use `...` (exclusive range) instead of `..` to prevent an off-by-one error. Here's one way: foo = (n) -> ((a) -> a).bind(null, i) for i in [0...n] Here's a more esoteric/bad style (but cute) way: foo = (n) -> (-> +@).bind i for i in [0...n]

I've been preferring explicit `map()`s in my CoffeeScript for exactly that reason.
Post reply on HN