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:
javascriptlet 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 +=:
javascriptlet res = 0;
for (let i = 1; i <= 100; i++) {
res += i;
}
console.log(res);
Задание 1
Find the sum of even numbers
from 2 to 100.
Задание 2
Find the sum of odd numbers
from 1 to 99.
Задание 3
Find the product of integers
from 1 to 20.