📜  如何仅对数组中的偶数求和 - 无论代码示例

📅  最后修改于: 2022-03-11 14:55:24.990000             🧑  作者: Mango

代码示例1
let numStr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const sumEvens = (numStr) => {
  let sum = 0;
  for (let i = 0; i < numStr.length; i++) {
    if (i % 2 === 0){ 
    sum = sum + numStr[i];
    }
  }
  return sum;
}
 
console.log(sumEvens(numStr));