diff --git a/README.md b/README.md index 51b0fe916..bec1ec155 100644 --- a/README.md +++ b/README.md @@ -2324,7 +2324,7 @@ remove([1, 2, 3, 4], n => n % 2 === 0); // [2, 4] Returns a random element from an array. -Use `Math.random()` to generate a random number, multiply it by `length` and round it of to the nearest whole number using `Math.floor()`. +Use `Math.random()` to generate a random number, multiply it by `length` and round it off to the nearest whole number using `Math.floor()`. This method also works with strings. ```js diff --git a/docs/index.html b/docs/index.html index 33fa8f86d..fb657e887 100644 --- a/docs/index.html +++ b/docs/index.html @@ -412,7 +412,7 @@ }, []) : [];
remove([1, 2, 3, 4], n => n % 2 === 0); // [2, 4]
-

sample

Returns a random element from an array.

Use Math.random() to generate a random number, multiply it by length and round it of to the nearest whole number using Math.floor(). This method also works with strings.

const sample = arr => arr[Math.floor(Math.random() * arr.length)];
+

sample

Returns a random element from an array.

Use Math.random() to generate a random number, multiply it by length and round it off to the nearest whole number using Math.floor(). This method also works with strings.

const sample = arr => arr[Math.floor(Math.random() * arr.length)];
 
sample([3, 7, 9, 11]); // 9
 

sampleSize

Gets n random elements at unique keys from array up to the size of array.

Shuffle the array using the Fisher-Yates algorithm. Use Array.prototype.slice() to get the first n elements. Omit the second argument, n to get only one element at random from the array.

const sampleSize = ([...arr], n = 1) => {
   let m = arr.length;