Skip to content

Commit 06218c6

Browse files
author
Charles Lecklider
committed
ignore arrays with fewer than 2 elements
1 parent b09c32c commit 06218c6

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Diff for: chapters/arrays/shuffling-array-elements.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ you are able to run it on any array you wish, in a much more direct manner.
8080

8181
{% highlight coffeescript %}
8282
do -> Array::shuffle ?= ->
83-
for i in [@length-1..1]
84-
j = Math.floor Math.random() * (i + 1)
85-
[@[i], @[j]] = [@[j], @[i]]
83+
if @length > 1
84+
for i in [@length-1..1]
85+
j = Math.floor Math.random() * (i + 1)
86+
[@[i], @[j]] = [@[j], @[i]]
8687
@
8788

8889
[1..9].shuffle()

0 commit comments

Comments
 (0)