diff --git a/README.md b/README.md index bf44e4cb0..b106fead2 100644 --- a/README.md +++ b/README.md @@ -874,7 +874,7 @@ Return `[-1]` if `length` of the array of indices is `0`, otherwise return the a const indexOfAll = (arr, val) => { const indices = []; arr.forEach((el, i) => el === val && indices.push(i)); - return indices.length ? indices : [-1]; + return indices; }; ``` @@ -883,7 +883,7 @@ const indexOfAll = (arr, val) => { ```js indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0,3] -indexOfAll([1, 2, 3], 4); // [-1] +indexOfAll([1, 2, 3], 4); // [] ``` diff --git a/docs/index.html b/docs/index.html index d32d03cc4..710b3d69a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -128,10 +128,10 @@ Object.assig

indexOfAll

Returns all indices of val in an array. If val never occurs, returns [-1].

Use Array.forEach() to loop over elements and Array.push() to store indices for matching elements. Return [-1] if length of the array of indices is 0, otherwise return the array of indices.

const indexOfAll = (arr, val) => {
   const indices = [];
   arr.forEach((el, i) => el === val && indices.push(i));
-  return indices.length ? indices : [-1];
+  return indices;
 };
 
indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0,3]
-indexOfAll([1, 2, 3], 4); // [-1]
+indexOfAll([1, 2, 3], 4); // []
 

initial

Returns all the elements of an array except the last one.

Use arr.slice(0,-1) to return all but the last element of the array.

const initial = arr => arr.slice(0, -1);
 
initial([1, 2, 3]); // [1,2]
 

initialize2DArray

Initializes a 2D array of given width and height and value.

Use Array.map() to generate h rows where each is a new array of size w initialize with value. If the value is not provided, default to null.

const initialize2DArray = (w, h, val = null) =>