JavaScript Basic Syntax Three

Source: Internet
Author: User

First, the circulation

Loop structure: An operation that executes repeatedly when a specific condition is met. Loop four elements: Loop variable, loop condition, loop increment, loop body.

First type: While loop. while (conditional expression) {loop body;}.

First step: Define the Loop variable

var count = 1;

Part II: Judging the cycle conditions

while (Count <= 5) {

Part III: Circulation body

Console.log ("Running Circle")

Fourth step: Loop increment

count++;

}

Use while to print a multiple of 1~100 that is not 7 and does not contain a number of 7

var num = 1;
While (num <=) {
var a = num% 7! = 0;
var b = num%! = 7;
var c = Math.floor (NUM/10)! = 7;
if (a && b && c) {
console.log (num);
                    }
num++;
                 }

The second type: Do-while cycle. do{Loop body}while (conditional expression).

var a = 0;
do{
Console.log (a);
a++;
}
while (a < 10)

The third type: For loop. for (loop variable loop condition loop increment) { loop body }

Use while to print a multiple of 1~100 that is not 7 and does not contain a number of 7

For (var i = 1; i <=; i++) {
var a = i% 7!=0;
var b = i%! = 7;
var c = Math.floor (I/10)! = 7;
if (a && b && c) {
Console.log (i)
                       }
                }

Second, break and continue.

bresk:1. In switch, used to end the entire branch statement.

2. In the loop, a break is encountered, the statement following it is no longer executed, and the entire loop is ended.

Continue: In the loop, the continue is encountered, and the statement behind it is not executed and jumps out of the loop.

Random number

Math.random (); Used to return a random number between a 0~1, including 0, but excluding 1.

1. Generate an integer randomly between [1,10]
var random = Math.floor (Math.random () * 10) + 1;
alert (random);

2. Generate an integer randomly between [1,100]

var random = Math.floor (Math.random () * +) + 1;
alert (random);

3. Generate an integer randomly between [10,20]

var random = Math.floor (Math.random () * one) + ten;
alert (random);

4. Generate an integer randomly between [12,38]

var random = Math.floor (Math.random () * 27) + 12;
alert (random);

5, Math.max to find the maximum value

var maxValue = Math.max (12,56,7987,121,54646,8975465);
alert (MaxValue);

6, Math.min to find the minimum value

var minValue = math.min (0,454,212,-1847,-21,42);
alert (MinValue);

7, Math.ceil Upward rounding
var value2 = Math.ceil (3.1);
alert (value2);

8. Math.Round Rounding

var value3 = Math.Round (4.2);
var value4 = Math.Round (5.8);
var a = Math.Round (4.4);
alert (VALUE3);
alert (VALUE4);
alert (a);

Iv. adding the specified content to an element

InnerHTML

Get element

var odiv = document.getElementById (' Div1 ');

InnerHTML: Adding the specified content to an element

odiv.innerhtml = "What to add"

JavaScript Basic Syntax Three

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.