JavaScript array implementation code _ javascript skills

Source: Internet
Author: User
Tags javascript array
Review the use of arrays in JS. For details about how to learn js arrays, refer. Truncated array: slice (startIndex, endIndex ):

Returns the array with the specified start position and end position. Does not change the content of the original array.

There is also an important method to insert, delete, or replace array elements:
Splice (startIndex, howice, element1 ~ ElementX): The original array content is modified. The parameter meanings are: "specify the start position", "How many elements are deleted (which can be 0)", and "new elements added to the array (starting with startIndex subscript )".

Related Applications
// Question 1: "A [B] C [D] E [F] G" is divided into two Arrays: ACEG and [B] [D] [F].

The Code is as follows:


Function QuestionFn1 (){
Var str = "A [B] C [D] E [F] G ";
Var oGetTwoArray = new GetTwoArray ();
// Obtain the value in the left and right brackets.
OGetTwoArray. GetLeftAndRightValue (str );
OGetTwoArray. GetNoLeftAndRightValue (str );
Var array1 = oGetTwoArray. arrBetweenLeftRight;
Var array2 = oGetTwoArray. arrNoLeftRight;
Alert (array1.join (""));
Alert (array2.join (""));
}
// Locate the index of the left and right brackets
Function GetTwoArray (){
This. indexLeft = 0; // left brace Index
This. indexRight = 0; // right brace Index
This. arrBetweenLeftRight = []; // array: contains letters in parentheses
This. arrNoLeftRight = []; // array: contains letters without parentheses
// Obtain the value in the left and right brackets.
This. GetLeftAndRightValue = function (str ){
// Locate the index of the left brace in the string
This. indexLeft = str. indexOf ('[');
This. indexRight = str. indexOf (']');
// Take the value in the brackets (including the brackets)
Var value = str. substring (this. indexLeft, this. indexRight + 1 );
// Store it in an array
This. arrBetweenLeftRight. push (value );
// The remaining str
Var restStr = str. substr (this. indexRight + 1 );
// If there are left and right parentheses, continue searching
If (restStr. indexOf ('[')! =-1 & restStr. indexOf (']')! =-1 ){
This. GetLeftAndRightValue (restStr );
}
}
// Obtain the value without left and right parentheses
This. GetNoLeftAndRightValue = function (str ){
// Locate the index of the left brace in the string
This. indexLeft = str. indexOf ('[');
This. indexRight = str. indexOf (']');
// Obtain the value without left and right parentheses (followed by the first right brace)
Var value = str. substring (0, 1 );
If (value! = '[') {// Because the first one may be left parentheses.
// Store it in an array
This. arrNoLeftRight. push (value );
}
// The remaining str
Var restStr = str. substr (this. indexRight + 1 );
// If there are left and right parentheses, continue searching
If (restStr. indexOf ('[')! =-1 & restStr. indexOf (']')! =-1 ){
This. GetNoLeftAndRightValue (restStr );
}
// If there are no left and right brackets left, add all the elements to the array.
Else if (restStr. indexOf ('[') =-1 & restStr. indexOf (']') =-1 ){
This. arrNoLeftRight. push (restStr );
}
}
}


// Question 2: There are two ordered integer arrays, for example, [1, 3, 5, 7, 9] and [2, 4, 6, 7, 8, 10, 13 ], design a function to merge the two arrays and remove repeated elements from the two arrays.

The Code is as follows:


Function QuestionFn2 (){
Var arr1 = [1, 3, 5, 7, 9];
Var arr2 = [2, 4, 6, 7, 8, 10, 13];
// Compare each element in loop arr2 with each element in arr1
For (var j = 0; j <arr2.length; j ++ ){
For (var I = 0; I <arr1.length; I ++ ){
// If the number in the second array is smaller than a certain number in the array, insert it to the front of it.
If (arr2 [j] <arr1 [I]) {
Arr1.splice (I, 0, arr2 [j]);
Break;
}
// Remove if the values are equal
Else if (arr2 [j] = arr1 [I]) {
Arr1.splice (I, 1, arr2 [j]);
Break;
}
// If arr2 [j] is greater than the last number in array 1, insert it to the end.
Else if (arr2 [j]> arr1 [arr1.length-1]) {
Arr1.push (arr2 [j]);
}
}
}
Alert (arr1.toString ());
}


// Question 3: Give an integer array, such as [2, 4, 5, 6, 7, 8] And a number, such as 10. Design a function to find two elements, make the sum of the two numbers and print them out.

The Code is as follows:


Function QuestionFn3 (){
Var sum = window. prompt ("Enter the sum of the two numbers in the array", 10 );
Var arr = [2, 4, 5, 6, 7, 8];
Var isFind = false; // whether the record exists
For (var I = 0; I <arr. length; I ++ ){
For (var j = I + 1; j <arr. length; j ++ ){
If (arr [j] + arr [I] = sum ){
Alert (arr [I] + "+" + arr [j] + "=" + sum );
IsFind = true;
}
}
}
// If no
If (! IsFind ){
Alert ("not adding two numbers in the array is equal to" + sum );
}
}


For more information about the javascript array, see
Javascript array summary using call Methods
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.