The front end of the third day javaScript based learning

Currently engaged in front-end work, particularly summary javaScript for them to learn the basics, I summarize the use of special dedicated small partners currently studying web front end, I hope you break their own piece of the sky in the field to the front, the software needs to sublime installation package, or front-end information can leave a message

First, loop
loop: Repeat a piece of code, an instruction execution meets (condition is false), the end of the loop

for循环,do while, while, for in语句

2.1 for loop

The for loop is a pre-test loop: first determine whether the conditions are met, when the condition is satisfied Perform loop. Until the condition is false ending loop.

for statements more flexible.

for (initial value of the loop variable; conditional expression; step) {

loop statement;

}

Principle: for the cycle to determine the conditional expression, when the conditional expression is true execution loop, out of the loop until the condition is false. Behind after the end of the loop for loop execution.

First performance:

And a first execution statement 1 is executed only once, then the determination condition statement 2, if the loop condition is satisfied, execution inwardly loop 3, 4 and then execute the statement change variable; if condition is not satisfied, the for loop, for loop back other js statements;

Second execution: 4 if the statement is executed, proceed to statement 2, it is determined whether the determination condition expression is satisfied, if the loop condition is satisfied, execution inwardly loop 3, 4 and then execute the statement change variable; if condition is not satisfied, the end for loop, perform additional js statement after the for loop;

……

When the condition is true, statement 2 is repeatedly performed, the statement 3, 4. The statement until the condition is false, execute other statements js later. (As long as you want to perform loop 3 must first determine that the condition 2)

According to the result for the principle of direct.

1 for(var i = 3; i < 13; i +=3 ) {

2 console.log(i);

3 }

4 console.log ( "End");

5 /*

6 of the first performance: i = 3, i <13 true, loop was executed, the output 3

7 the second execution: i = 6, i <13 true, execution loop, the output 6

8 third execution: i = 9, i <13 true, execution loop, the output 9

9 Fourth performed: i = 12, i <13 true, execution loop, the output 12

10 Fifth performed: i = 15, i <13 false, when the loop condition is false, only end for

11 for cycle ends before performing other js statement back

12 output "end"

13 */

for loop variable called loop variable, it can be any name, usually we are used to writing i, j, k

Loop variable is a global variable. You can write anywhere, can write for circulation outside or inside.

1 // 2 loop variable is a global variable, not a loop it is possible to perform

2 where i = 3;

3 for(console.log(++ i); i < 1 ; i ++) {

4 console.log(i);

5 }

6 /*

7 first implementation: i = 3, only once output a statement 4, i <1 false end for

8 */

Statement 2 can be written as any conditional expression, as long as it can be judged true or false.

1 // statements can be written into an arbitrary form 2

2 for (var i = 4; in! = 10; i + = 3) {

3 console.log(i);

4 }

5 /*

6 of the first performance: i = 4, i = 10 true, execution loop output 4!

7 the second execution: i = 7, i = 10 true, execution loop output 7!

8 third execution: i = 10, i = 10 false, an immediate end to the cycle!

9 */

Loop cycle may have been, this is an endless loop.

1 // When the loop condition is true, the loop will always execute

2 for (var i = 1; i> = 0; i ++) {

3 console.log(i);

4 }

1 for(var i = 3; i < 10 ; i += 3) {

2 console.log(i);

3 }

4 console.log(i);

5 /*

6 of the first performance: i = 3, i <10 true output into the loop 3

7 the second execution: i = 6, i <10 enters the true output loop 6

8 third execution: i = 9, i <10 enters the true output loop 9

9 Fourth performed: i = 12, i <10 false immediate end loop

Back 10 to perform other statements js

11 console.log(i)12

12 */

Inside the loop for loop statement can also be written as an if statement, if you want to perform structure must meet the conditions for cycling conditions and if statements.

1 for(var i = 4; i < 25 ; i +=4) {

When 2 // i <25, while it outputs i i be divisible by 5

3 if(i % 5 == 0) {

4 console.log(i);

5 }

6 }

7 console.log(i);

8 /*

9 of the first performance: i = 4, i <25 true, into the loop determines if i% 5 == 0 false End if statement 3;

10 second execution: i = 8, i <25 true, into the loop determines if i% 5 == 0 false End if statement 3;

11 third execution: i = 12, i <25 true, into the loop determines if i% 5 == 0 false End if statement 3;

12 Fourth performed: i = 16, i <25 true, into the loop determines if i% 5 == 0 false End if statement 3;

13 Fifth performed: i = 20, i <25 true, into the loop determines if i% 5 == 0 if true execute statement output 20;

14 Sixth performed: i = 24, i <25 true, into the loop determines if i% 5 == 0 false End if statement

15 Seventh executed: i = 28, i <25 false end for loop

16 behind the output statements to perform other js i = 28

17 */

for loops for nested loop, layer by layer, to perform, for each layer are different loop variable.

1 for(var i = 1 ; i < 4; i ++) {

2 for(var j = 1; j < 4 ; j ++) {

3 console.log(i,j);

4 }

5 }

6 console.log(i,j);

7 /*

The first 8: i = 1, i <4 true, into the loop j = 1, j <4 true, (1,1), j ++

9 j=2,j<4真,(1,2),j++

10 j=3,j<4真,(1,3),j++

11 j = 4, j <4 j loop ends prosthesis

* Of 12 times: i = 2, i <4 true, into the loop, j = 1, j <4 true (2,1) j ++

13 j=2,j<4真,(2,2),j++

14 j=3,j<4真,(2,3),j++

15 j = 4, j <4 j loop ends prosthesis

16

* Of 17 times: i = 3, i <4 true, into the loop, j = 1, j <4 true (3,1) j ++

18 j=2,j<4真,(3,2),j++

19 j=3,j<4真,(3,3),j++

20 j = 4, j <4 j loop ends prosthesis

21

* Of 22 times: i = 4, i <4 false, i loop ends. Back to perform other statements js

23 outputs (i = 4, j = 4)

24 */

2.2 exhaustive idea

Concept: We often want to get a set of data, there are some specific requirements, the computer can not help our own output data. Our people need to write a program that lets your computer to help us implement the program. All possible cases, enumerated one by one, and then we define human determination conditions, to give it a qualified data output is not satisfied then skip the next data continues to verify whether the condition is satisfied until all the possible cases We have a verification times. This method is called full-lift method, also called brute-force method.

Outer: enumerate, for circulation.

Inner layer: judge, if statement.

Case 1: Output all about numbers in a number of consoles.

About several concepts: a% b == 0, it is called a multiple of b, b is called a divisor or factor.

Divisor of a number, the minimum is 1 and the maximum is itself.

1 // user enters a positive number, all about the number of output this number

2 // 12 such as user input divisor 1,2,3,4,6,12

3 // to user input

4 var num = parseInt (prompt ( "Enter a number"));

5 // output divisors

Number range of about 6 // Interval 1- numbers themselves, these possibilities to enumerate

7 for (var i = 1; i <= num; i ++) {

8 // only output divisor

9 // divisible

10 if(num % i == 0) {

11 // This number is about i, output

12 console.log(i);

13 }

14 }

2.3 Multiplier Accumulator and tired

Accumulator: Sometimes we do not want to know when calculating straightforward process, just want the final value, to meet the conditions of a result from plus 1.

note:

An accumulator variable, must be written for the outside (against each variable is reset to 0), the initial value is set to zero.

2 When the value satisfying the condition, from the accumulator plus 1

3 The final results must be written on the outside of the for loop.

1 // total number of digital input, the user about the number of output

2 // to the user's input

3 var num = parseInt (prompt ( "Enter a number"));

4 // total number of output

// accumulator 5 must be written out of an initial value for 0

6 was some = 0;

7 // enumerate all possibilities

8 for (var i = 1; i <= num; i ++) {

9 if(num % i == 0) {

10 // i is the number of num about, since the accumulator plus 1

11 sum ++;

12 }

13 }

14 // accumulator final result for end output

15 console.log(sum);

Tired multiplier: We do not want the middle of the process, I think what we need to get the final product

1 multiplicative variables, must be written for the outside (against each variable is reset to 1), an initial value setting.

2 When the value satisfying the condition, the accumulator squaring

3 The final results must be written on the outside of the for loop.

Factorial:! = 4 example 4 . 3 2 * 1

1 // user digital input, outputs the factorial of the number of

2 // to the user's input

3 var num = parseInt (prompt ( "Enter a number"));

4 // output factorial

@ 5 must be tired by the writing for the outside loop, the initial value is 1

6 var cheng = 1;

7 // factorial 1-num

8 for (var i = 1; i <= num; i ++) {

9 // factorial

10 cheng *= i;

11 }

12 // results in final output

13 console.log (cheng);

Case: Users enter a number, this number is judged not a prime number

Prime: 1 and in addition to his own two divisor no other divisors. For example, 3

Composite number: 1 and in addition to his own two about a few other divisors. For example, 4

1 is neither prime nor composite.

1 // to user input

2 var num = parseInt (prompt ( "Enter a number"));

// find about 3 Total number of

4 // accumulators, variables must be written on the outer loop initial value of 0 for

5 was some = 0;

All possibilities the number 6 // num about to enumerate for loop

7 for (var i = 1; i <= num; i ++) {

8 // find divisors

9 if(num % i === 0) {

10 // i num is a divisor of

The total number of 11 //

12 sum ++;

13 }

14 }

// 15 final sum value output for the outside loop

16 // console.log(sum);

@ 17 is not a prime number is determined

18 // sum is 2 then if num is a prime number

19 if(sum === 2) {

20 console.log (+ "is a prime number" num);

21 }else {

22 console.log (+ "is not a prime number" num);

23 }

Guess you like

Origin blog.51cto.com/14334627/2408635