From c557338b02658880d8b893ded3d3c3611e29d67c Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sun, 31 Dec 2017 17:26:28 +0530 Subject: [PATCH 1/4] add sampleSize --- snippets/sampleSize.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/sampleSize.md diff --git a/snippets/sampleSize.md b/snippets/sampleSize.md new file mode 100644 index 000000000..ef4ca3e63 --- /dev/null +++ b/snippets/sampleSize.md @@ -0,0 +1,19 @@ +### sampleSize + +Returns the length of string. + +```js +const sampleSize = (arr,n=1) => { + let m = arr.length; + while (m) { + const i = Math.floor(Math.random() * m--); + [arr[m], arr[i]] = [arr[i], arr[m]]; + } + return arr.slice(0,n) +} +``` + +```js +sampleSize([1,2,3],2); // [3,1] +sampleSize([1,2,3],4); // [2,3,1] +``` From 6bbdf72875bb2744962e49fe4b7a7df93943bb10 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sun, 31 Dec 2017 17:26:28 +0530 Subject: [PATCH 2/4] add sampleSize --- snippets/sampleSize.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/sampleSize.md diff --git a/snippets/sampleSize.md b/snippets/sampleSize.md new file mode 100644 index 000000000..6b6721b52 --- /dev/null +++ b/snippets/sampleSize.md @@ -0,0 +1,19 @@ +### sampleSize + +Gets `n` random elements at unique keys from `array` up to the size of `array`. + +```js +const sampleSize = (arr,n=1) => { + let m = arr.length; + while (m) { + const i = Math.floor(Math.random() * m--); + [arr[m], arr[i]] = [arr[i], arr[m]]; + } + return arr.slice(0,n) +} +``` + +```js +sampleSize([1,2,3],2); // [3,1] +sampleSize([1,2,3],4); // [2,3,1] +``` From c9eed5848316cbe0cd0c31ff9d1e7284a4d5b7da Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 14:09:45 +0200 Subject: [PATCH 3/4] Update sampleSize.md --- snippets/sampleSize.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/snippets/sampleSize.md b/snippets/sampleSize.md index 6b6721b52..95e26a643 100644 --- a/snippets/sampleSize.md +++ b/snippets/sampleSize.md @@ -2,8 +2,12 @@ Gets `n` random elements at unique keys from `array` up to the size of `array`. +Shuffle the array using the [Fisher-Yates algorithm](https://github.com/chalarangelo/30-seconds-of-code#shuffle). +Use `Array.slice()` to get the first `n` elements. +Omit the second arguent, `n` to get only one element at random from the array. + ```js -const sampleSize = (arr,n=1) => { +const sampleSize = ([...arr],n=1) => { let m = arr.length; while (m) { const i = Math.floor(Math.random() * m--); From 105a5e3867673ac5edc2f82b61c90aee12471c5e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 14:10:46 +0200 Subject: [PATCH 4/4] Update sampleSize.md --- snippets/sampleSize.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/sampleSize.md b/snippets/sampleSize.md index 95e26a643..f6187bdea 100644 --- a/snippets/sampleSize.md +++ b/snippets/sampleSize.md @@ -4,7 +4,7 @@ Gets `n` random elements at unique keys from `array` up to the size of `array`. Shuffle the array using the [Fisher-Yates algorithm](https://github.com/chalarangelo/30-seconds-of-code#shuffle). Use `Array.slice()` to get the first `n` elements. -Omit the second arguent, `n` to get only one element at random from the array. +Omit the second argument, `n` to get only one element at random from the array. ```js const sampleSize = ([...arr],n=1) => {