The task is:
Given an array of digitals numbers, return a new array of length number containing the last even numbers from the original array (in the same order).
Codewars compiler shows the "Execution Timed Out (12000 ms)" error, though the code is working as intended. Please help to optimize my code, because I can't figure it out myself
My code:
function evenNumbers(array, number) {
for (let i=0; i < array.length; i++) {
if (array[i] % 2 != 0) {
array.splice(i, 1);
i -= 1;
}
}
array.splice(0, array.length - number)
return array;
}