如何使用 for 或 while 循环迭代到 100 并使用 javascript 显示每次迭代的总和?
How to iterate to 100 using a for or while loop and show the sum of each iteration with javascript?
刚做个小作业。我需要迭代到 100,而且 console.log 之前每个示例的结果。
系列示例:(1)+(1+2)+(1+2+3)+…+(1+2+3+…+n)<=100
Iteracion1=1
Iteracion2= 1+2 = 3
iteracion 3: 1+2+3 = 6
iteracion 4: 1+2+3+4 = 10
我有这个:
for (i = 0; i <= 100; i++) {
if(i < 100) {
console.log(`${i}+`);
}};
但我不知道如何在每次迭代中添加它的总和。如果你对此有任何参考,那就太好了!谢谢。
这应该没问题。
我为 i
的迭代长度创建了一个新数组,并使用 Array#reduce
将其加总为一个数字。
const max = 10;
for (let i = 1; i <= max; i++) {
const arr = new Array(i).fill().map((_, i) => i + 1);
console.log(`Iteration ${i}: ${arr.join('+')} = ${arr.reduce((acc, b) => acc + b, 0)}`);
}
您可以使用 for 之外的变量来实现此目的
let sum = 0;
const previousSums = [];
for (i = 0; i < 100; i++) {
previousSums.push(sum);
sum += i;
console.log(`${previousSums}`);
}
创建一个可以添加索引的数组。
创建一个函数来计算数组中数字的总和。我在这里使用了reduce
。
Create a string,然后记录一下。
const arr = [];
function sum(arr) {
return arr.reduce((acc, c) => acc + c, 0);
}
// On each iteration push the index in to the
// array. Create a string that joins up the array
// elements and logs the result of the sum
for (let i = 1; i <= 10; i++) {
arr.push(i);
const str = `Iteration ${i}: ${arr.join('+')} = ${sum(arr)}`;
console.log(str);
};
您可以efficiently
使用单个循环获得结果。
出于演示目的,我打印了 20
。您可以添加您选择的任意数量。
let lastTotal = 0;
let lastStr = "";
for (let i = 1; i <= 10; ++i) {
const total = (lastTotal ?? 0) + i;
const str = lastStr ? lastStr + " + " + i : i;
console.log(`Iteration ${i}: ${str}${total === 1 ? "" : " = " + total}`);
lastTotal = total;
lastStr = str;
}
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
var total = 0;
var res = 0;
var upto = 6;
for(i=1;i<=upto;i++){
total = total+i;
res = res+total;
}
console.log(res);
刚做个小作业。我需要迭代到 100,而且 console.log 之前每个示例的结果。
系列示例:(1)+(1+2)+(1+2+3)+…+(1+2+3+…+n)<=100
Iteracion1=1
Iteracion2= 1+2 = 3
iteracion 3: 1+2+3 = 6
iteracion 4: 1+2+3+4 = 10
我有这个:
for (i = 0; i <= 100; i++) {
if(i < 100) {
console.log(`${i}+`);
}};
但我不知道如何在每次迭代中添加它的总和。如果你对此有任何参考,那就太好了!谢谢。
这应该没问题。
我为 i
的迭代长度创建了一个新数组,并使用 Array#reduce
将其加总为一个数字。
const max = 10;
for (let i = 1; i <= max; i++) {
const arr = new Array(i).fill().map((_, i) => i + 1);
console.log(`Iteration ${i}: ${arr.join('+')} = ${arr.reduce((acc, b) => acc + b, 0)}`);
}
您可以使用 for 之外的变量来实现此目的
let sum = 0;
const previousSums = [];
for (i = 0; i < 100; i++) {
previousSums.push(sum);
sum += i;
console.log(`${previousSums}`);
}
创建一个可以添加索引的数组。
创建一个函数来计算数组中数字的总和。我在这里使用了
reduce
。Create a string,然后记录一下。
const arr = [];
function sum(arr) {
return arr.reduce((acc, c) => acc + c, 0);
}
// On each iteration push the index in to the
// array. Create a string that joins up the array
// elements and logs the result of the sum
for (let i = 1; i <= 10; i++) {
arr.push(i);
const str = `Iteration ${i}: ${arr.join('+')} = ${sum(arr)}`;
console.log(str);
};
您可以efficiently
使用单个循环获得结果。
出于演示目的,我打印了 20
。您可以添加您选择的任意数量。
let lastTotal = 0;
let lastStr = "";
for (let i = 1; i <= 10; ++i) {
const total = (lastTotal ?? 0) + i;
const str = lastStr ? lastStr + " + " + i : i;
console.log(`Iteration ${i}: ${str}${total === 1 ? "" : " = " + total}`);
lastTotal = total;
lastStr = str;
}
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
var total = 0;
var res = 0;
var upto = 6;
for(i=1;i<=upto;i++){
total = total+i;
res = res+total;
}
console.log(res);