如何将for循环中的偶数和奇数结果总和都推送到一个数组中?

huangapple go评论62阅读模式
英文:

How to push both sum of even and odd result from for loop into an array?

问题

使用for循环从0到100进行迭代,并打印所有偶数的和以及所有奇数的和。以数组形式打印偶数的和和奇数的和

输出: [2550, 2500]

let sumOfEven = 0;
let EvenOddArr = [];
for (let i = 0; i <= 100; i += 2) {
  sumOfEven += i;
}

let sumOfOdd = 0;
for (let i = 1; i <= 100; i += 2) {
  sumOfOdd += i;
}
EvenOddArr.push(sumOfEven, sumOfOdd);
console.log(EvenOddArr);
英文:

Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds. Print sum of evens and sum of odds as array

Output: [2550, 2500]

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let sumOfEven = 0;
let EvenOddArr = [];
for (let i = 0; i &lt;= 100; i += 2) {
  sumOfEven += i;
}
console.log(sumOfEven);

let sumOfOdd = 0;
for (let i = 1; i &lt;= 100; i += 2) {
  sumOfOdd += i;
}
console.log(sumOfOdd);
console.log(EvenOddArr);

<!-- end snippet -->

答案1

得分: 3

&lt;!-- 开始代码片段: js 隐藏: false 控制台: true Babel: null --&gt;

&lt;!-- 语言: lang-js --&gt;

let sumOfEven = 0;
let sumOfOdd = 0;

for (let i = 0; i &lt;= 100; i++) {
  if (i % 2 === 0) {
     sumOfEven += i;
  } else {
     sumOfOdd += i;
  }
}

let evenOddArr = [sumOfEven, sumOfOdd];  
console.log(evenOddArr);

&lt;!-- 结束代码片段 --&gt;
英文:

An easy-to-understand version:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

let sumOfEven = 0;
let sumOfOdd = 0;

for (let i = 0; i &lt;= 100; i++) {
  if (i % 2 === 0) {
     sumOfEven += i;
  } else {
     sumOfOdd += i;
  }
}

let evenOddArr = [sumOfEven, sumOfOdd];  
console.log(evenOddArr);

<!-- end snippet -->

答案2

得分: 2

你差不多到了 - 你只需要一些 push 操作

let sumOfEven = 0;
let EvenOddArr = [];
for (let i = 0; i <= 100; i += 2) {
  sumOfEven += i;
}
EvenOddArr.push(sumOfEven)

let sumOfOdd = 0;
for (let i = 1; i <= 100; i += 2) {
  sumOfOdd += i;
}
EvenOddArr.push(sumOfOdd)

console.log(EvenOddArr);
英文:

You're nearly there - all you need is a couple of pushes

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let sumOfEven = 0;
let EvenOddArr = [];
for (let i = 0; i &lt;= 100; i += 2) {
  sumOfEven += i;
}
EvenOddArr.push(sumOfEven)

let sumOfOdd = 0;
for (let i = 1; i &lt;= 100; i += 2) {
  sumOfOdd += i;
}
EvenOddArr.push(sumOfOdd)

console.log(EvenOddArr);

<!-- end snippet -->

答案3

得分: 2

你可以将两个数的余数作为数组的索引。

const evenOddArr = [0, 0];

for (let i = 0; i <= 100; i++) evenOddArr[i % 2] += i;

console.log(evenOddArr);
英文:

You could take the remainder of two as index for the array.

<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->
const evenOddArr = [0, 0];

for (let i = 0; i &lt;= 100; i++) evenOddArr[i % 2] += i;

console.log(evenOddArr);

<!-- end snippet -->

答案4

得分: 2

console.log(Array(101).fill().reduce((a,_,i)=>(a[i%2]+=i,a),[0,0]))

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(Array(101).fill().reduce((a,_,i)=&gt;(a[i%2]+=i,a),[0,0]))

<!-- end snippet -->

答案5

得分: 1

这是一个更为简洁的选择,当你对 JavaScript 有了更多了解后:

let sumArr = Array.from({ length: 101 })
  .reduce((acc, _, i) => (acc[i % 2] += i, acc), [0, 0]);

console.log(sumArr);
英文:

Here is an alternative for when you have studied JS a bit more

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let sumArr = Array.from({ length: 101 })
  .reduce((acc,_,i) =&gt; (acc[i % 2] += i, acc), [0, 0]);

console.log(sumArr);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 21:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定