loop every other number in array javascript code example
Example 1: get every other item in an array
var myArray = ["First", "Second", "Third", "Fourth", "Fifth"]
function every_other(array){
var temporaryArray = []
for (var i = 0; i < array.length; i += 2){
temporaryArray.push(array[i])
}
return temporaryArray.join(", ")
}
console.log(every_other(myArray))
Example 2: get every other item in an array
var myArray = ["First", "Second", "Third", "Fourth", "Fifth"]
function every_other(array){
var temporaryArray = []
for (var i = 1; i < array.length; i += 2){
temporaryArray.push(array[i])
}
return temporaryArray.join(", ")
}
console.log(every_other(myArray))