1let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2
3// filter out all elements that are located at an even index in the array.
4
5let x = arr.filter((element, index) => {
6 return index % 2 === 0;
7})
8
9console.log(x)
10// [1, 3, 5, 7, 9]
1// If you want every even index value:
2var myArray = ["First", "Second", "Third", "Fourth", "Fifth"]
3function every_other(array){
4 var temporaryArray = []
5 for (var i = 1; i < array.length; i += 2){ //Add two to i every iteration
6 temporaryArray.push(array[i]) //Add the element at index i to a temporary array
7 }
8 return temporaryArray.join(", ")
9}
10console.log(every_other(myArray)) //Expected output: Second, Fourth
1// If you want every odd index value:
2var myArray = ["First", "Second", "Third", "Fourth", "Fifth"]
3function every_other(array){
4 var temporaryArray = []
5 for (var i = 0; i < array.length; i += 2){ //Add two to i every iteration
6 temporaryArray.push(array[i]) //Add the element at index i to a temporary array
7 }
8 return temporaryArray.join(", ")
9}
10console.log(every_other(myArray)) //Expected output: First, Third, Fifth