JavaScript loops and Arrays common operations

Source: Internet
Author: User
Tags array length





While loop


Grammar:


Do and loop


Syntax: do{loop body}while (conditional expression);
Features: The Do While loop does not matter whether the condition is true or not, in any case the loop body executes once.
Use occasions: User input password, if the password is not 123456, continue to enter the password until the password is correct
Example: do {var psd = prompt ("Enter password");} while (PSD! = "123456");


For loop


Syntax: for (statement 1; Statement 2; statement 3) {Loop body}
In general: statement 1 write Loop increment declaration, Statement 2 write loop judgment condition, statement 3 write loop increment change


Break and Continue


Break is encountered in the loop and will jump out of the loop
Continue//English translation to continue. The loop encountered continue will end the current loop, and the entire loop will still wait until the loop condition is not established or a break is encountered to end the entire loop.
Example: calculates the number of integers between 1-100 except that can be divisible by 7.
var sum = 0;for (var i = 1; i <=; i++) {if (i% 7 = = 0) {continue;} sum + = i;} Console.log (sum);


Nested loops


As long as the code in accordance with the JS language can be used as a loop body,


Array


Declaration of an array: 1, declared with a constructor (if you declare something with the new keyword, it is a constructor.) ), syntax var array name = new Array ();
3, enter how to store the data, using subscript index, subscript: Starting from 0 to accumulate an integer syntax: array name [subscript] = value; arr[0] = 10;
4, the value is indexed using subscript. Console.log (Arr[0]); Console.log (arr[1]);//If there is no value labeled 1, the undefined is obtained.
5, modify the value, use subscript index arr[0] = 100;console.log (arr[0]); A value of 100 Note: the subscript can actually use a string, provided it is the content itself is a number, for example: "10"; converts to a value of 10 note: If you declare an array like this, and the parentheses have only one value of type A, that value is the length of the array, If a value is not a number type, this value is the value and will not be the length of the array. var arr2 = new Array, Consolie.log (ARR2), and the resulting result is arr2.length (10).
Commonly used declarative method: var arr4 = [10,20,30,40,50]; still find the corresponding subscript value starting from 0, for example, I want to take 30 of the value Console.log (arr4[2]);
Arrays in 6,js are somewhat different from arrays in other languages, and the arrays in JS can hold arbitrary values. Varr ARR5 = ["123", 10,true,undefined,null,function Test () {alert ("haha")}]; Although the array in JS can hold any type of value, it is not recommended.
The length of the array in JS can be changed.


The length of the array


1, the length of the array is the number of elements stored in the array, with length to get, we can modify the length of the value, so as to change the size of the array. Example: var arr1 = [10,20,30,40,50]arr1.length = 10; The value of the element after which no value is given is undefined.
2, if the value of the array length is changed to small
var arr2 = [10,20,30,40,50]arr2.length = 3; The following 2 values are deleted, if you want to get arr2[4], the result is undefined, and if the length of the array is restored, the value cannot be restored.
3. Empty array var arr3 = [1,2,3,4]; The first method: Modify the length of the array to 0; the second method: You can make this array point to an empty array arr3 = [];
4, get the array last subscript equals the length-1.5 of the array, use the length of the array to add new elements


Traversal of an array


1. Using the For loop forward traversal
2. Use for loop reflection traversal



Cases:


1 var arr = [10,20,30,40,50];
2 for (var i = arr.length-1; i> = 0; i-) {
3 console.log (arr [i]);
4} // Get the result [50,40,30,20,10] 


Two ways to flip an array


var arr = [10,20,30,40,50];
         for (var i = 0; i <arr.length / 2; i ++) {
           // i can control the number of cycles, that is, the number of exchanges
         // How to exchange subscript bit 0 and arr.length-1-0
           // 1 and arr.length-1-1
           // 2 and arr.length-1-2

           var temp = arr [i];
           arr [i] = arr [arr.length-1-i];
           arr [arr.length-1-i] = temp;

         }
         console.log (arr);




// second
// Use the backward traversal of the array, and pack each element into the new array.
Var arr = [10,20,30,40,50];
Var arrNew = [];
For (var i = arr.length-1; i> = 0; i--) {
ArrNew [arrNew.length] = arr [i];
}
Console.log (arrNew); 








JavaScript loops and Arrays common operations


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.