let numbers = [4, 5, 7, 8, 14, 45, 76];
function getEvenElements(arr) {
let evenArray = [];
for (let i = 0; i < arr.length / 2; i++) {
evenArray.push(arr[2 * i]);
}
return evenArray.filter(num => num !== undefined);
}
alert(getEvenElements(numbers));
http://jsbin.com/unocar/2/edit
I attempted to extract only the even-indexed elements from an array using this code. It seems to be working, but it includes empty values as well. How can I modify the code to output existing elements only?