Problem
- Several people are standing in a row divided into two teams.
- The first person goes into team 1, the second goes into team 2, the third goes into team 1, and so on.- 첫 번째 사람은 팀 1에 들어가고, 두 번째 사람은 팀 2에 들어가고, 세 번째 사람은 팀 1에 들어가는 식으로 진행된다.
 
- Given an array of positive integers, return a new array of two integers, where the first one is the total weight of team 1, and the second one is the total weight of team 2.- 새로운 두 정수의 배열을 반환한다.
- 첫 번째 정수는 팀 1의 총 가중치이고, 두 번째 정수는 팀 2의 총 가중치이다.
 
Solution 01
function rowWeights(arr) {
  let t1 = 0;
  let t2 = 0;
  
  for (let i = 0; i < arr.length; i++) {
    if (i % 2 === 0) {
      t1 += arr[i];
    } else {
      t2 += arr[i];
    }
  }
  return [t1, t2];
}
rowWeights([1, 2, 3, 4]);  // [4, 6]
rowWeights([5, 6, 3, 2]);  // [8, 8]
Solution 02
function rowWeights(arr) {
  let result = [0, 0];
  
  for (let i = 0; i < arr.length; i++) {
    i % 2 === 0 ? result[0] += arr[i] : result[1] += arr[i];
  }
  return result;
}
rowWeights([1, 2, 3, 4]);  // [4, 6]
rowWeights([5, 6, 3, 2]);  // [8, 8]
Solution 03
function rowWeights(arr) {
  let t1 = arr.filter((i, index) => index % 2 === 0).reduce((result, i) => result + i, 0);
  let t2 = arr.filter((i, index) => index % 2 !== 0).reduce((result, i) => result + i, 0);
  
  return [t1, t2];
}
rowWeights([1, 2, 3, 4]);  // [4, 6]
rowWeights([5, 6, 3, 2]);  // [8, 8]
filter(): 테스트를 통과한 배열의 각 값을 모아, 새 배열로 반환한다.
reduce(): 배열을 하나의 값으로 줄이고, 그 값을 반환한다.
Solution 04
function rowWeights(arr) {
  return arr.reduce((result, i, index) => (result[index % 2] += i, result), [0, 0]);
}
rowWeights([1, 2, 3, 4]);  // [4, 6]
rowWeights([5, 6, 3, 2]);  // [8, 8]