Use of javascript array, javascript Array

Source: Internet
Author: User

Use of javascript array, javascript Array
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> untitled document </title>
</Head>
<Script type = "text/javascript">
// Basic array usage
// The type of javascript elements can be freely stored.
Var arr1 = [2, 4, 6, 8, 10];
Var sum = 0;
For (var I = 0; I <arr1.length; I ++ ){
Sum + = arr1 [I];
}
Document. write ("sum =" + sum + "; avg =" + sum/arr1.length );
// Dynamic growth of js Array
Var a = [2, 3];
A [2] = "hello ";
Document. write ("<br/>" + a [2]);
// Common attributes and functions of Arrays
Var str = "hello world ";
Var arr = str. split ("");
Document. write ("<br/>" + arr + "<br/>" + arr. length );
// Array Summary
/*
① Arrays can store any type of data
② The array size can be dynamically increased without having to be specified in advance
③ The array name can be understood as a reference to the first address of the array.
④ Array elements start from 0 by default.
*/
// Two-dimensional array usage
Var ew1 = [[1, 4, 9], [4, 'Hello'];
For (var I = 0; I <ew1.length; I ++ ){
Var ele = ew1 [I];
For (var j = 0; j <ele. length; j ++ ){
Document. write ("<br/>" + ele [I]);
}
}
Document. write ("<br/> js bubble sort method <br/> ");
Var so = [2, 9, 3, 8, 4, 7, 5,-4];
Var tem = 0;
For (var I = 0; I <so. length; I ++ ){
For (var j = 0; j <so. length; j ++ ){
If (so [I]> so [j]) {
Tem = so [I];
So [I] = so [j];
So [j] = tem;
}
}
}
Document. write (so );
Document. write ("<br/> js sequential search <br/> ");
Var fin = [2,-5,100,-];
Var findval = 4;
Var flag = false;
For (var I = 0; I <fin. length; I ++ ){
If (findval = fin [I]) {
Document. write ("the subscript for finding this number is:" + I );
Flag = true;
}
}
If (! Flag ){
Document. write ("query fruitless ");
}
Document. write ("<br/> js Binary Search <br/> ");
/*
Core Ideas:
1. Binary Search should ensure that our array is an ordered one.
2. First, find the number in the middle of the array and compare it with the number you want to query.
① The number you want to query is larger than the number in the middle. It indicates that we should query the right side of the array
② The number you want to query is smaller than the number in the middle. It means that we should query the left side of the array.
③ The number you want to query is equal to the number in the middle.
*/
Var ef = [2, 4, 6, 8, 9, 15];
Function searc (findVal, ef, leftindex, rightindex ){
// Exit condition
If (leftindex> rightindex ){
Document. write ("query fruitless ");
Return;
}
Var middleindex = Math. round (leftindex + rightindex)/2 );
If (ef [middleindex] <findVal ){
Searc (findVal, ef, middleindex + 1, rightindex );
} Else if (ef [middleindex]> findVal ){
Searc (findVal, ef, leftindex, middleindex-1 );
} Else {
Document. write ("found, subscript is" + middleindex );
}
}
Searc (4, ef, 0, ef. length-1 );
</Script>
<Body>
</Body>

</Html>

If you have any mistakes or suggestions, please join us and learn to make friends.

QQ: 1327880701


Copyright statement: original post of the blogger. For details, refer to the source. Http://blog.csdn.net/dzy21

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.