math what is a alternating series code example
Example: Alternating Sums
const alternatingSums = array => {
let team1 = 0, team2 = 0
array.forEach((num, index) => {
if(index % 2 === 0) team1 += num
else team2 += num
})
return [team1, team2]
}
/*
// OR METHOD 2
*/
const alternatingSums = array => {
let team1 = [], team2 = []
array.forEach((num, index) => {
if(index % 2 === 0) team1.push(num)
else team2.push(num)
})
team1 = team1.reduce((a,b) => a + b, 0)
team2 = team2.reduce((a,b) => a + b, 0)
return [team1, team2]
}