Thursday, February 6, 2014

My path to JavaScript: Refactoring fizzbuzz with Array.map and Array.join

I've just refactored the generalized version of FizzBuzz I posted about recently using Array.map and Array.join functions.

This is the version that I had before:


and this is the new version using Array.map and Array.join:


Even though it was fun coding that "map+join" recursive function of the first version, the second version is much simpler and more efficient (I used jsPerf to compare the two versions, check out the comparison).

Had I known Array.map and Array.join before, I could have saved a lot of typing and thinking to "reinvent the whell" :)

No comments:

Post a Comment