Implementation steps: 1. Define a variable assigned a value of 0 to store the summation result, the syntax "var sum=0;"; 2. Use the for statement to traverse all integers between 100 and 1000, the syntax " for (var i = 100; i
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In javascript, you can use the for loop statement to find the sum from 100 to 1000.
Implementation steps
Step 1: Define a variable assigned a value of 0 to store the summation result
var sum=0;
Step 2: Use the for statement to traverse all integers between 100 and 1000
for (var i = 100; i <= 1000; i++) { //循环体语句块; }
Step 3: In the loop body, use the "=" operator to Add all the traversed values
sum += i;
After the loop ends, the value of the variable sum is the sum of 100 to 1000.
Implementation code:
var sum=0; for (var i = 100; i <= 1000; i++) { sum += i ; } console.log("100到1000的和为"+sum);
Encapsulate it to realize the sum of the specified interval
function f(a,b){ var sum=0; for (var i = a; i <= b; i++) { sum += i ; } console.log(a+"到"+b+"的和为"+sum); }
Find the sum of 1~2
f(1,2);
Find the sum of 1~5
f(1,5);
The sum of 1~10
f(1,10);
Extended knowledge: for loop
for loop will control the loop The variable of times is predefined in the for statement, so the for loop statement can perform loop operations according to the known number of loops, which is suitable for situations where the number of times the script needs to be run is clearly known.
The syntax format of the for loop is as follows:
for (初始化语句; 循环条件; 变量更新--自增或自减) { 语句块; }
The for loop statement can be disassembled into 4 parts: three of the ()
Expressions and "statement blocks" in {}
, let's analyze them below.
Statement analysis:
Initialization statement (expression 1): mainly initializes a variable value, used to set a counter, that is, the value at the beginning of the loop ; This statement is only executed during the first loop and will not be executed again in the future.
Loop condition (expression 2): Restriction condition for loop execution, used to control whether to execute the code in the loop body; if the condition is TRUE, the loop continues, if the condition is FALSE , the loop ends and exits the loop immediately.
Variable update (expression 3): an expression with an increment or decrement operation. Every time the loop is executed, the value of the counter is immediately modified so that the loop The conditions gradually become "untenable".
Statement block: Several codes that need to be executed when the condition is judged to be true.
Is the above description a bit convoluted? Let’s take a look at the execution flow chart of the for loop statement to understand the execution of the for loop more intuitively. Process:
[Related recommendations: javascript video tutorial, programming video】
The above is the detailed content of How to find the sum of 100 to 1000 in javascript. For more information, please follow other related articles on the PHP Chinese website!