This is the 11th day of my participation in Gwen Challenge
Accumulator and multiplicator
Accumulator: Sometimes when we calculate, we don’t want to know the direct process, but just want the final value. If there is a result that meets the condition, we add 1.
Note:
The 1 accumulator variable, which must be written outside for (in case the variable is reset to 0 each time), starts at 0.
2 When there is a value that meets the condition, the accumulator increments itself by 1
3 The final result must also be written outside the for loop.
// The user enters a number and outputs the total number of divisor
// input to the user
var num = parseInt(prompt("Please enter a number"));
// Output the total number
// The accumulator must be written outside for with an initial value of 0
var sum = 0;
// List all the possibilities
for(var i = 1; i <= num ; i ++) {
if(num % i == 0) {
// I is a divisor of num. The accumulator increments itself by 1sum ++; }}// The final result of the accumulator is output at the end of for
console.log(sum);
Copy the code
Tired multiplier: we don’t want the intermediate process, we want to get the final product
The 1 multiplier variable, which must be written outside for (in case the variable is reset to 1 each time), is initially set to 1.
2 When there is a value satisfying the condition, the accumulator multiplicates itself
3 The final result must also be written outside the for loop.
Factorial: like 4! = 432 * 1
// The user enters a number and outputs the factorial of the number
// input to the user
var num = parseInt(prompt("Please enter a number"));
// Output factorial
// The multiplier must be written outside the for loop, with an initial value of 1
var cheng = 1;
/ / factorial 1 - num
for(var i = 1 ; i <= num ; i ++) {
/ / factorial
cheng *= i;
}
// The result is in the final output
console.log(cheng);
Copy the code
Example: The user enters a number and determines whether it is prime
Prime number: no divisors except 1 and itself. Such as 3
Composite number: there are other divisors besides 1 and itself. Such as 4
1 is neither prime nor composite.
// input to the user
var num = parseInt(prompt("Please enter a number"));
// find the total number of divisor
// the variable must be written outside the for loop with an initial value of 0
var sum = 0;
// all possibilities of num divisor are enumerated in the for loop
for(var i = 1 ; i <= num; i ++) {
/ / to find a few
if(num % i === 0) {
// I is a divisor of num
/ / the total numbersum ++; }}// The final sum value is printed outside the for loop
// console.log(sum);
// Check whether it is prime
Num is a prime number
if(sum === 2) {
console.log(num + "Is prime.");
}else {
console.log(num + "Not prime.");
}
Copy the code
do while
Do while test the loop statement, execute the loop statement first, then evaluate the exit, if the condition is true, execute the loop statement again…
If the condition is false, end the loop and execute the other JS statements that follow
Note: Loop variables must be written outside to prevent them from being emptied.
Grammar:do{loop statement; }while(Conditional expression)Copy the code
// Loop variables must be written outside the do while statement
var i = 3;
do {
console.log(i);
i += 3;
}while(i < 10);
Copy the code
The do while loop is executed at least once.
// The do while loop is executed at least once
// Loop variables must be written outside
var i = 3;
do {
console.log(i ++);
i += 2;
}while(i > 8)
console.log(i);
/* I =3, I ++, 3, I increment (I = 6); Perform exit judgment I > 8 false end cycle. Execute other js statements following the loop and output I = 6; * /
Copy the code
while
While statement: pretests the loop statement by checking the condition and executing the loop when the condition is true
Note: Loop variables must be written outside to prevent them from being emptied.
while(conditional expression) {loop statement; }Copy the code
// Loop variables must be written outside the statement
var i = 3;
while(i < 10) {
console.log(i);
i += 3;
}
/* I =3, I <10 true, enter the loop, output 3, I =6, I =6, I <10 true, enter the loop again, output 6, I =9, I =9, I =12, I <10 false, end the loop */
Copy the code
All while can be written with for.
For example:
// Change to the for statement
for(var i = 3; i < 10 ; i += 3) {
console.log(i);
}
/* I =3, I <10 true, enter the loop output 3 I =6, I <10 true, enter the loop output 9 I =12, I <10 false, end the loop */
Copy the code
The position of the while output statement is very important
The position of the output statement is very important
// Loop variables must be written outside the statement
var i = 3;
while(i < 10) {
i += 3;
console.log(i);
}
/* I =3, I <10 true, enter the loop I =6, output 6 I =6, I <10 true, enter the loop I =9, output 9 I =9, I <10 true, enter the loop I =12, output 12 I =12, I <10 false, end the loop */
Copy the code
Looping statements
break
Break: indicates that we have found a value that satisfies the condition and no longer need to proceed, immediately ending the loop.
Before the loop stops, the only possibility is that the condition is false and the loop ends immediately. You can now also end the loop with a break
// Find a number divisible by 3 by iterating from 1 to 200
for(var i = 1 ; i <= 200 ; i ++) {
/ / limit
if(i % 3= =0) {
// The loop statement breaks to terminate the loop immediately
break;
}
console.log(i);
}
// The statement after the for loop
console.log("The end");
/* I =1, I <=200 true, I %3==0 false break, output 1 I =2, I <=200 true, output 1 I %3==0 false break, output 2 I =3, I <=200 true, output 1 I %3==0 I %3==0 execute break, end loop with break, give all control of statement to other js statement */ after for
Copy the code
Results:
Break function: execute break in loop statement for,while,do while, switch statement, end the whole loop statement (SWICH statement) at the break layer, and hand over the control of the statement to other JS statements after the loop statement.
Cannot end the outer loop if there are nested statements.
for(var i = 1; i < 4 ; i ++) {
for(var j = 1; j < 4 ; j ++) {
// end the loop when j==2
if(j == 2) {
// break can only control the loop of the layer. Break can only control j, but not I
break;
}
console.log(i,j); }}console.log(i,j);
/* I =1, I <4 true, enter the first layer I loop j=1,j<4 true enter the second layer j loop; 1,1 j=2,j<4 true,j ==2,break (j=2,j<4 true,j ==2, j==2, j==2) j=1; J <4 true, enter the j loop, do not execute break, output 2,1 j=2,j<4 true, enter the J loop, execute break to end the whole j loop I =3,(3,1) output (3,2)j==2 end the loop, do not output (3,2) I = 4, I <4 false, end the I loop, Give control to the js statement following I (I =4,j=2) */
Copy the code
If break wants to control the outer loop, it can bind with the tag.
waiceng: for(var i = 1; i < 4 ; i ++) {
for(var j = 1; j < 4 ; j ++) {
if(j == 2) {
// generate a binding break from the tag to control only I but not j
// Execute break to terminate the loop of I
break waiceng;
}
console.log(i,j); }}console.log(i,j);
/* I =1, I <4 J =1,j<4 execute j loop statement j==2 if false will not execute break, output (1,1) j=2,j<4 execute J loop statement j==2 if true, execute break waicheng; End the I loop and give control to the other JS statements following the for loop (I =1,j=2) */
Copy the code
continue
Continue, continue to
This is not what I want, so let’s go to the next loop (test the value immediately).
Continue can end only one loop, not the entire loop
for(var i = 1; i < 10 ; i += 2) {
/ / limit
if(i % 3= =0) {
continue;
}
console.log(i);
}
/* I =1, I <10 true, execute loop statement; If I %3==0, do not execute continue; Output 1 I =3, I <10 true, execute loop statement; If I %3==0 is true, execute continue to end the loop with this value; I =5, I <10 true, execute loop statement, I %3==0 false continue, print 5 I =7, I <10 true, execute loop statement; I %3==0 if false, continue 7 if I =9, I <10 if true, execute loop statement; If I %3==0 is true, execute continue to end the loop with this value; Immediately enter the next test I =11, I <10 false, end for loop */
Copy the code
Continue: Execute a continue in the statement for,while,do while to terminate the continue layer and immediately proceed to the next value test. It’s not closing the loop
Cannot end the outer loop if there are nested statements.
for(var i = 1; i < 4 ; i ++) {
for(var j = 1; j < 4 ; j ++) {
/ / limit
if(j == 2) {
// continue can only control j++, not I
continue;
}
console.log(i,j); }}/* I =1,j=1, execute j loop; If j==2 is false, do not execute continue, print 1,1, j=2, execute j loop; If j==2 is true, execute continue to end the loop with j=2 and enter j++ j=3 to execute the j loop. J ==2,j= 2,j= 2 Execute continue to end the loop with this value j=3, output 2,3 j=4,j<4 is false end j loop I =3, output (3,1)(3,3) I =4, I <4 is false end I loop */
Copy the code
Continue can also control the outer loop with tags.
waiceng: for(var i = 1; i < 4 ; i ++) {
for(var j = 1; j < 4 ; j ++) {
if(j == 2) {
// continue controls the loop of I, cannot control j
continue waiceng;
}
console.log(i,j); }}/* I =1, I <4 J =1,j<4 really execute j cycle; If j==2 is false, continue is not executed, and output 1,1,j =2,j<4 is executed. If j==2 is true, execute continue to end the loop I =1. If I <4, execute the loop I; j=1,j<4; Execute the J loop; J ==2 false, output 2,1 j=2,j<4 true execute j loop; If j==2 is true, execute continue to end the loop I =2 I =3, I <4, print (3,1) execute continue to end the loop with the value I =3, it will not print 3,2 I =4, I <4 false, end the loop */
Copy the code
Break, continue can simplify computer computation.
Prime numbers: Check 2- if there are any other divisor between the square root of the number, if not then this number is prime, if there is not a prime number
// input to the user
var num = parseInt(prompt("Please enter a number"));
/ / determine
// Check if there are any other divisor between 2 and the square root. If you find one, you can conclude that the number is not prime
// Enumerate all possible numbers of 2- square
for(var i = 2; i <= Math.sqrt(num); i ++) {
// If you find a divisor, it is not prime
if(num % i == 0) {
// I is a divisor of Num
console.log(num + "Not prime.");
break; }}Copy the code
Print all primes up to 1000
// Print primes up to 1000
// 1 list all possibilities 1-1000for
waiceng: for(var i = 2; i<= 1000 ; i ++) {
// 2 limits the output of prime numbers
// I = 12, output if 12 is prime, skip if 12 is not prime
for(var j = 2; j <= Math.sqrt(i); j ++) {
// check whether j is a divisor of I
if(i % j == 0) {
// j is a divisor, I is not prime, not we want to check I ++
continuewaiceng; }}// Continue is never executed, so I is prime
console.log(i);
}
Copy the code