diff --git a/README.md b/README.md index 6fc1215f1..c7cfc7723 100644 --- a/README.md +++ b/README.md @@ -1979,11 +1979,11 @@ none([0, 0, 0]); // true Returns the nth element of an array. Use `Array.slice()` to get an array containing the nth element at the first place. -If the index is out of bounds, return `[]`. +If the index is out of bounds, return `undefined`. Omit the second argument, `n`, to get the first element of the array. ```js -const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0]; +const nthElement = (arr, n = 0) => (n === -1 ? arr.slice(n) : arr.slice(n, n + 1))[0]; ```
diff --git a/docs/array.html b/docs/array.html index 8e9865463..e7b1e855e 100644 --- a/docs/array.html +++ b/docs/array.html @@ -290,7 +290,7 @@

none

Returns true if the provided predicate function returns false for all elements in a collection, false otherwise.

Use Array.some() to test if any elements in the collection return true based on fn. Omit the second argument, fn, to use Boolean as a default.

const none = (arr, fn = Boolean) => !arr.some(fn);
 
none([0, 1, 3, 0], x => x == 2); // true
 none([0, 0, 0]); // true
-

nthElement

Returns the nth element of an array.

Use Array.slice() to get an array containing the nth element at the first place. If the index is out of bounds, return []. Omit the second argument, n, to get the first element of the array.

const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
+

nthElement

Returns the nth element of an array.

Use Array.slice() to get an array containing the nth element at the first place. If the index is out of bounds, return undefined. Omit the second argument, n, to get the first element of the array.

const nthElement = (arr, n = 0) => (n === -1 ? arr.slice(n) : arr.slice(n, n + 1))[0];
 
nthElement(['a', 'b', 'c'], 1); // 'b'
 nthElement(['a', 'b', 'b'], -3); // 'a'
 

offset

Moves the specified amount of elements to the end of the array.

Use Array.slice() twice to get the elements after the specified index and the elements before that. Use the spread operator(...) to combine the two into one array. If offset is negative, the elements will be moved from end to start.

const offset = (arr, offset) => [...arr.slice(offset), ...arr.slice(0, offset)];
diff --git a/docs/beginner.html b/docs/beginner.html
index 6983a3e1a..f84bb8a41 100644
--- a/docs/beginner.html
+++ b/docs/beginner.html
@@ -125,7 +125,7 @@
 

minN

Returns the n minimum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in ascending order).

Use Array.sort() combined with the spread operator (...) to create a shallow clone of the array and sort it in ascending order. Use Array.slice() to get the specified number of elements. Omit the second argument, n, to get a one-element array.

const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
 
minN([1, 2, 3]); // [1]
 minN([1, 2, 3], 2); // [1,2]
-

nthElement

Returns the nth element of an array.

Use Array.slice() to get an array containing the nth element at the first place. If the index is out of bounds, return []. Omit the second argument, n, to get the first element of the array.

const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
+

nthElement

Returns the nth element of an array.

Use Array.slice() to get an array containing the nth element at the first place. If the index is out of bounds, return undefined. Omit the second argument, n, to get the first element of the array.

const nthElement = (arr, n = 0) => (n === -1 ? arr.slice(n) : arr.slice(n, n + 1))[0];
 
nthElement(['a', 'b', 'c'], 1); // 'b'
 nthElement(['a', 'b', 'b'], -3); // 'a'
 

offset

Moves the specified amount of elements to the end of the array.

Use Array.slice() twice to get the elements after the specified index and the elements before that. Use the spread operator(...) to combine the two into one array. If offset is negative, the elements will be moved from end to start.

const offset = (arr, offset) => [...arr.slice(offset), ...arr.slice(0, offset)];