Loop structure 【while, do while, for】

Execute a piece of code repeatedly

A. while B. while C. while D. while

1And the initial valuewhile(2And conditions) {3, execute code4, iteration}Copy the code

B.【do while

1And the initial valuedo{
                3, execute the code4, iteration}while(2And conditions)Copy the code

Note:Start by executing the code in DOthenJudge conditions

[C]

The for loop is essentially the same as the while loop, which makes the structure clearer by putting the initial judgment and iteration in a special place

for(1, initial value;2And conditions;3Iteration) {4, execute the code}Copy the code

Perform:

                1-->2(Judge whether the establishment of the establishment of the implementation4)
                4-->3
                3-->2
                2-->4. until2Don't set upCopy the code

In general we use a while loop when the number of loops is not fixed and a for loop when the number of loops is fixed

The four parts of the for loop can be omitted

Continue: Ends the current loop and proceeds to the next loop

    for(var i=0; i<10; i++){if(i==5)
           continue
           console.log(i);
        }
Copy the code

Break: ends the current loop

    for(var i =0; i<10; i++){if(i==5)
        break;
        console.log(i);
    }
Copy the code

Use break to break out of the double-layer loop

Add a label outer and use break outer;

    outer: for(var i =0; i<10; i++){for(var j =0; j<i; j++){if(j==3) {break outer;
               }
          document.write("*   ");
       }
       document.write("<br>");
   }
Copy the code

(In the actual development process, loop statement is often used, must master!!)

Walk through the classic case

 var arr1 = [1.3.3.3.3.3.2.2.2.1];
    // Find the most common element in arr1; And how many times?
    // 1: array deduplication
    function deletArr(arr) {
        // Initialize the result
        var res = [];  // The result of the operation, not a duplicate array
        // De-process the parameter arr
        for (var i = 0; i < arr.length; i++) {
            if (res.indexOf(arr[i]) === -1) {
                res.push(arr[i])
            }
        }
        return res
    }
    var arr = deletArr(arr1);
    console.log(arr);


    // 2: turns the array into an object, and the value of the key element is initially 1
    function arrChangeObj(arr) {
        // Initialize the result of the operation
        var res = {};
        for (var i = 0; i < arr.length; i++) {
            res[arr[i]] = 1
        }
        return res; // Outputs the result of the operation outside the function
    }

    var obj = arrChangeObj(arr);
    console.log(obj);

    // 3: indicates the number of occurrences of key in arR1 and assigns value

    for (var k in obj) {
        var count = 0; / / counter
        for (var i = 0; i < arr1.length; i++) {
            k = Number(k);
            if(arr1[i] === k) { count++; obj[k] = count; }}}console.log(obj);



    // 4: Find the maximum value of all values in the object.
    var maxValue = 0; // Assume maximum value
    var maxX = 0;     / / record key
    for (var x in obj) {
        if (obj[x] > maxValue) {
            maxValue = obj[x]; 
            maxX = x
        }
    }
    // Find the corresponding key based on the value
    console.log(maxValue, maxX);
Copy the code