How to find the sum of 100 to 1000 in javascript

青灯夜游
Release: 2022-10-12 18:09:36
Original
1808 people have browsed it

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

How to find the sum of 100 to 1000 in javascript

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;
Copy after login

Step 2: Use the for statement to traverse all integers between 100 and 1000

for (var i = 100; i <= 1000; i++) {
    //循环体语句块;   
}
Copy after login

Step 3: In the loop body, use the "=" operator to Add all the traversed values

sum += i;
Copy after login

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);
Copy after login

How to find the sum of 100 to 1000 in javascript

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);
}
Copy after login

Find the sum of 1~2

f(1,2);
Copy after login

How to find the sum of 100 to 1000 in javascript

Find the sum of 1~5

f(1,5);
Copy after login

How to find the sum of 100 to 1000 in javascript

The sum of 1~10

f(1,10);
Copy after login

How to find the sum of 100 to 1000 in javascript

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 (初始化语句; 循环条件; 变量更新--自增或自减) {
    语句块;   
}
Copy after login

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:

How to find the sum of 100 to 1000 in javascript

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!