problems(js): add “2634. Filter Elements from Array”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
d18930ad7e
commit
56d6a05ce4
1 changed files with 17 additions and 0 deletions
17
problems/filter-elements-from-array.js
Normal file
17
problems/filter-elements-from-array.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
/**
|
||||||
|
* @param {number[]} arr
|
||||||
|
* @param {Function} fn
|
||||||
|
* @return {number[]}
|
||||||
|
*/
|
||||||
|
var filter = function(arr, fn) {
|
||||||
|
let new_arr = new Array();
|
||||||
|
|
||||||
|
let i = 0;
|
||||||
|
for (let x of arr) {
|
||||||
|
if (fn(x, i++)) {
|
||||||
|
new_arr.push(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new_arr;
|
||||||
|
};
|
Loading…
Reference in a new issue