Skip to content

Commit

Permalink
Merge git://github.com/malgorithms/coffeescript-cookbook.github.com i…
Browse files Browse the repository at this point in the history
…nto malgorithms-master
  • Loading branch information
sukima committed Dec 5, 2014
2 parents 42e3340 + 16debf2 commit 291275c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions chapters/arrays/shuffling-array-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ the algorithm.

{% highlight coffeescript %}
shuffle = (a) ->
# From the end of the list to the beginning, pick element `i`.
for i in [a.length-1..1]
# Choose random element `j` to the front of `i` to swap with.
j = Math.floor Math.random() * (i + 1)
# Swap `j` with `i`, using destructured assignment
[a[i], a[j]] = [a[j], a[i]]
if a.length >= 2
# From the end of the list to the beginning, pick element `i`.
for i in [a.length-1..1]
# Choose random element `j` to the front of `i` to swap with.
j = Math.floor Math.random() * (i + 1)
# Swap `j` with `i`, using destructured assignment
[a[i], a[j]] = [a[j], a[i]]
# Return the shuffled array.
a

Expand Down

0 comments on commit 291275c

Please sign in to comment.