This commit is contained in:
Rohit Tanwar
2018-01-06 16:41:42 +05:30
committed by GitHub
parent ca298e5389
commit c500de403d

View File

@ -4,10 +4,11 @@ Returns an array of indexes at which the `val` occurs in `arr`. If it occurs onl
``` js ``` js
const indexOfAll = (arr, val) => { const indexOfAll = (arr, val) => {
let indexes = [], i; const indices = [];
arr.forEach((el,i) => {if(el === val) indexes.push(i)}) arr.forEach((el, i) => el === val && indices.push(i))
return indexes.length === 0 ? [-1] : indexes return indices.length ? indices : [-1];
} };
``` ```
``` js ``` js
indexOfAll([1,2,3],1); // [0] indexOfAll([1,2,3],1); // [0]