Skip to content

Commit 55ee0a8

Browse files
committed
Update array references
1 parent b11b613 commit 55ee0a8

File tree

1 file changed

+90
-17
lines changed

1 file changed

+90
-17
lines changed

src/utilities/array_functions.js

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,28 +183,101 @@ p5.prototype.shorten = function(list) {
183183
};
184184

185185
/**
186-
* Randomizes the order of the elements of an array. Implements
187-
* <a href='http://Bost.Ocks.org/mike/shuffle/' target=_blank>
188-
* Fisher-Yates Shuffle Algorithm</a>.
186+
* Shuffles the elements of an array.
187+
*
188+
* The first parameter, `array`, is the array to be shuffled. For example,
189+
* calling `shuffle(myArray)` will shuffle the elements of `myArray`. By
190+
* default, the original array won’t be modified. Instead, a copy will be
191+
* created, shuffled, and returned.
192+
*
193+
* The second parameter, `modify`, is optional. If `true` is passed, as in
194+
* `shuffle(myArray, true)`, then the array will be shuffled in place without
195+
* making a copy.
189196
*
190197
* @method shuffle
191-
* @param {Array} array Array to shuffle
192-
* @param {Boolean} [bool] modify passed array
193-
* @return {Array} shuffled Array
198+
* @param {Array} array array to shuffle.
199+
* @param {Boolean} [bool] if `true`, shuffle the original array in place. Defaults to `false`.
200+
* @return {Array} shuffled array.
201+
*
194202
* @example
195-
* <div><code>
203+
* <div>
204+
* <code>
205+
* function setup() {
206+
* createCanvas(100, 100);
207+
*
208+
* background(200);
209+
*
210+
* // Create an array of colors.
211+
* let colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
212+
*
213+
* // Create a shuffled copy of the array.
214+
* let shuffledColors = shuffle(colors);
215+
*
216+
* // Draw a row of circles using the original array.
217+
* for (let i = 0; i < colors.length; i += 1) {
218+
* // Calculate the x-coordinate.
219+
* let x = (i + 1) * 12.5;
220+
*
221+
* // Style the circle.
222+
* let c = colors[i];
223+
* fill(c);
224+
*
225+
* // Draw the circle.
226+
* circle(x, 33, 10);
227+
* }
228+
*
229+
* // Draw a row of circles using the original array.
230+
* for (let i = 0; i < shuffledColors.length; i += 1) {
231+
* // Calculate the x-coordinate.
232+
* let x = (i + 1) * 12.5;
233+
*
234+
* // Style the circle.
235+
* let c = shuffledColors[i];
236+
* fill(c);
237+
*
238+
* // Draw the circle.
239+
* circle(x, 67, 10);
240+
* }
241+
*
242+
* describe(
243+
* 'Two rows of circles on a gray background. The top row follows the color sequence ROYGBIV. The bottom row has all the same colors but they are shuffled.'
244+
* );
245+
* }
246+
* </code>
247+
* </div>
248+
*
249+
* <div>
250+
* <code>
196251
* function setup() {
197-
* let regularArr = ['ABC', 'def', createVector(), TAU, Math.E];
198-
* print(regularArr);
199-
* shuffle(regularArr, true); // force modifications to passed array
200-
* print(regularArr);
201-
*
202-
* // By default shuffle() returns a shuffled cloned array:
203-
* let newArr = shuffle(regularArr);
204-
* print(regularArr);
205-
* print(newArr);
252+
* createCanvas(100, 100);
253+
*
254+
* background(200);
255+
*
256+
* // Create an array of colors.
257+
* let colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
258+
*
259+
* // Shuffle the array.
260+
* shuffle(colors, true);
261+
*
262+
* // Draw a row of circles using the original array.
263+
* for (let i = 0; i < colors.length; i += 1) {
264+
* // Calculate the x-coordinate.
265+
* let x = (i + 1) * 12.5;
266+
*
267+
* // Style the circle.
268+
* let c = colors[i];
269+
* fill(c);
270+
*
271+
* // Draw the circle.
272+
* circle(x, 50, 10);
273+
* }
274+
*
275+
* describe(
276+
* 'A row of colorful circles on a gray background. Their sequence changes each time the sketch runs.'
277+
* );
206278
* }
207-
* </code></div>
279+
* </code>
280+
* </div>
208281
*/
209282
p5.prototype.shuffle = function(arr, bool) {
210283
const isView = ArrayBuffer && ArrayBuffer.isView && ArrayBuffer.isView(arr);

0 commit comments

Comments
 (0)