function filter (arr, fn) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (fn(arr[i])) {
newArr.push(arr[i]);
}
};
return newArr;
}
var input = [1, 2, 3, 4, 5, 6];
function isEven(x) { return x % 2 == 0; } // проверяет на четность
console.log(filter(input, isEven)); // [2, 4, 6]
console.log(input); // [1, 2, 3, 4, 5, 6]
ZnVuY3Rpb24gZmlsdGVyIChhcnIsIGZuKSB7Cgl2YXIgbmV3QXJyID0gW107CgoJZm9yICh2YXIgaSA9IDA7IGkgPCBhcnIubGVuZ3RoOyBpKyspIHsKCQlpZiAoZm4oYXJyW2ldKSkgewoJCQluZXdBcnIucHVzaChhcnJbaV0pOwoJCX0gCgl9OwoJcmV0dXJuIG5ld0FycjsKfQoKCnZhciBpbnB1dCA9IFsxLCAyLCAzLCA0LCA1LCA2XTsKZnVuY3Rpb24gaXNFdmVuKHgpIHsgcmV0dXJuIHggJSAyID09IDA7IH0gLy8g0L/RgNC+0LLQtdGA0Y/QtdGCINC90LAg0YfQtdGC0L3QvtGB0YLRjApjb25zb2xlLmxvZyhmaWx0ZXIoaW5wdXQsIGlzRXZlbikpOyAvLyBbMiwgNCwgNl0KCmNvbnNvbGUubG9nKGlucHV0KTsgLy8gWzEsIDIsIDMsIDQsIDUsIDZd