英文:
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 <= 100; i += 2) {
sumOfEven += i;
}
console.log(sumOfEven);
let sumOfOdd = 0;
for (let i = 1; i <= 100; i += 2) {
sumOfOdd += i;
}
console.log(sumOfOdd);
console.log(EvenOddArr);
<!-- end snippet -->
答案1
得分: 3
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: null -->
<!-- 语言: lang-js -->
let sumOfEven = 0;
let sumOfOdd = 0;
for (let i = 0; i <= 100; i++) {
if (i % 2 === 0) {
sumOfEven += i;
} else {
sumOfOdd += i;
}
}
let evenOddArr = [sumOfEven, sumOfOdd];
console.log(evenOddArr);
<!-- 结束代码片段 -->
英文:
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 <= 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 push
es
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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);
<!-- 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 <= 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)=>(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) => (acc[i % 2] += i, acc), [0, 0]);
console.log(sumArr);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论