JavaScript-- gallop script execution's Web page (2)

Fourth, the flow control statements

1. branch

if statement

condition indicates any expression, the evaluation result of the expression is not necessarily boolean boolean if not, the ECMAScript calls Boolean () conversion function to convert the result of a Boolean expression , when the value is true , the execution if the contents of the code block.

 

 

if-else statements

When the condition is true , the execution if the contents of the code blocks, otherwise, else content code block, in general, if the block code is only one line, the braces can be omitted.

 

 

if-else if-else statements, such as:

if(condition1){

statement1

} else if(condition2){

statement2

} else {

statement3

}

Multiple conditional branching, when condition1 is true , the execution statement1 , or when condition2 is true executed when statement2 , when condition1, condition2 are false Executes statement3 .

 

switch statement

expression can be a variable can also be an expression, when expression === Choice , execution of the current case of code code block. Each case code block must contain BREAK , it represents the current contents of executing the code block out switch block. When all the case the case is not satisfied, execution default content code block. default position can be freely .

 

 

2. cycle

Loop three elements: initial conditions, the end of the determination condition, iteration.

Initializer: initialization value, usually digital, will only be done once. Cycle may be written in vitro

exit-condition: end conditions, typically using the logical operators for determining the end of the cycle. Before each iteration of the loop will execute the code

final-expression: each execution of the loop After execution codes, it is generally closer to the iteration termination condition

for loop

 

 

while loop

 

(Loop body code before being executed, will be evaluated on the export conditions. Therefore, the body of the loop code is likely never be executed)

 

do-while loop

 

(Code only after the execution of the loop, will test outlet conditions. Thus, a minimum of code within the loop body is executed once)

 

3. To end the current cycle keywords

break keyword

If you want to exit before all iterations, you can use BREAK . When performing break , the loop will immediately jump, execute the following code.

continue keyword

And break the difference is, the Continue not out of the loop , but the end of the current cycle, the next cycle to enter.

( Supplementary note, the ECMAScript block-level scope is not present, variables defined within the loop can be accessed from the outside )

 

4. branched, cyclic application of classic

(1 ) were used while / do-while / for loop to achieve 10 factorial.

var mul = 1;

/*

var i = 2;

while(i <= 10) {

  many * = i;

  i++;

}

console.log("mul: ",mul);

*/

/*

var i = 2;

do {

  many * = i;

  i++;

}while(i <= 10);

console.log("mul: ",mul);

*/

/*

for(var i=2; i<=10; i++) {

  many * = i;

}

console.log("mul: ",mul);

*/

 

(2) print multiplication table.

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

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

    console.log(num + "*" + i + "=" + num*i + (num*i<10?”  ”:” ”));

  }

  console.log();

}

 

(3 ) there are 1 , 2 , 3 , 4 digits, how many other with no repeat of the three-digit numbers can be composed? How much are?

var i = 0; // save the number to one hundred

var j = 0; // save the number to ten

var k = 0; // save the digital bits

var count = 0; // save the digital number

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

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

    for(k = 1; k <= 4; k++) {

      if(i != j && j != k && i!= k) {

        count += 1;

        console.log(i*100+j*10+k);

      }

    }

  }

}

console.log ( " total composition can be "  + COUNT + " numbers ");

 

(4) Analyzing 101-200 number among prime numbers, and the output of all primes (only by 1 and itself a natural number divisible by a prime number) .

were count = 0;

for(var i=101; i<= 200; i++) {

  for(var j=2; j<i; j++) {

    if(i % j == 0) {

      break; // just in addition to a number so that i can be divisible to come out of the loop

    }else {

      if(j == i-1) {

        console.log (i); // addition to a number not finished out of the last cycle, it can be determined that the number is a prime number

        count++;

      }

    }

  }

}

console.log ( "101-200 total between " + count + "primes");

 

( 5 ) Output 100-1000 in all "narcissistic number." ( So-called "Narcissus number" refers to a three-digit, which is a bit number equal to the number of cubic se, for example: 153 is a "Narcissus number", since 153 = 1 cubic square + 5 cubed + 3 cube ).

for(var i=100; i<1000; i++) {

  each of g = 10%;

  where S = / 10% 10;

  where b = a / 100;

  var sum = g*g*g + s*s*s + b*b*b;

 

  if(sum == i) {

    console.log (i + " is the number of Narcissus ");

  }

}

 

(6 ) The decomposition of the quality factor a positive integer. For example: the input 90, to print out 90 = 3 * 2 * 3 * 5 .

There are n = 90;

console.log("n=");

 

for(var i=2; i<n; i++) {

  while (n! = i) { // recycled to the i ended when the maximum prime factor

    if (n% i == 0) {// i is a prime factor

      console.log(i,"*");

      n = n / i; // after addition of the quality factor increasingly smaller until the maximum prime factor equal to

    } else {// i is not a prime factor

      break;

    }

  }

}

console.log (n); // n values have become the largest prime factor, as the last multiplier

 

(7) any two positive integers find the greatest common divisor and least common multiple, for example 48 and 72 .

There are a = 48;

var b = 72;

There are c = 0;

 

if (a <b) {// always make the maximum number of A , the minimum number of b

  c = a;

       a = b;

       b = c;

}

 

// loop to a and b with the addition c first remainder are 0 the end

c = b; // c from the minimum number between two numbers themselves decrements

while(!(a%c==0 && b%c==0)) {

  c--;

}

 

console.log ( "their greatest common divisor is:" + c);

console.log ( "least common multiple thereof:" + (a * b / c));

 

(8 ) Determine 1000 completely within a few (if a natural number, other than just the removal of its own and all factors are equal, this number is called perfect numbers)

var y; // y as a factor

var sum; // sum for recording factor and

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

  sum = 1; // 1 is a complete number, from 1 after the start determination

  // factor 1 has been recorded in the sum , the so y from 2 recycled to the maximum factor ( I / 2 )

  for(y = 2; y<=(i/2); y++) {

    if (i% y == 0) {// y is i time factor, and accumulation factors

      sum += y;

    }

  }

  if (sum == i) {// factor and a i is equal to a full number itself

    console.log(i);

  }

}

the console.log ( " more to 1000 or less complete number ");

 

Guess you like

Origin www.cnblogs.com/wodeqiyuan/p/11408563.html