Skip to content

Commit

Permalink
Merge branch 'invisnet-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sukima committed Dec 5, 2014
2 parents d2c97b2 + 319ca25 commit cb456b2
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions chapters/arrays/shuffling-array-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ The following code adds the shuffle function to the Array prototype, which means
you are able to run it on any array you wish, in a much more direct manner.

{% highlight coffeescript %}
do -> Array::shuffle ?= ->
for i in [@length-1..1]
Array::shuffle ?= ->
if @length > 1 then for i in [@length-1..1]
j = Math.floor Math.random() * (i + 1)
[@[i], @[j]] = [@[j], @[i]]
@
this

[1..9].shuffle()
# => [ 3, 1, 5, 6, 4, 8, 2, 9, 7 ]
Expand All @@ -97,8 +97,10 @@ objects you don’t own][dontown]; [Extending built-in native objects. Evil or n
operator (`?=`). That way, we don't overwrite someone else's, or a native browser method.

Also, if you think you'll be using a lot of these utility functions, consider using a
utility library, like [Lo-dash](http://lodash.com/). They include a lot of nifty
utility library, like [Lo-dash](http://lodash.com/)^†. They include a lot of nifty
features, like maps and forEach, in a cross-browser, lean, high-performance way.

^† [Underscore](http://underscorejs.org/) is also a good alternative to Lo-dash.

[dontown]: http://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/
[extendevil]: http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/

0 comments on commit cb456b2

Please sign in to comment.