Accumulation of numbers in loops in JavaScript
Let's use the loop to find the sum of integers
from 1 to 100. To solve such a
problem, numbers are iterated over in loop and
their sum is sequentially written to some variable:
let res = 0;
for (let i = 1; i <= 100; i++) {
res = res + i;
}
console.log(res); // the required sum
You can simplify the solution
through the operator +=:
let res = 0;
for (let i = 1; i <= 100; i++) {
res += i;
}
console.log(res);
Find the sum of even numbers
from 2 to 100.
Find the sum of odd numbers
from 1 to 99.
Find the product of integers
from 1 to 20.