problems(js): add “2634. Filter Elements from Array”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-05-12 17:31:44 +02:00
parent d18930ad7e
commit 56d6a05ce4
Signed by: mfocko
GPG key ID: 7C47D46246790496

View 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;
};