JS Review 1

Source: Internet
Author: User

1, the Regular:
/^[0-9a-zz-z_]*&///Match 0 to more, + is 1 to multiple,? 0 or more,. Any value
/^[0-9a-zz-z_]{6,}&///accurate to at least 6 bits
\w Finding Word characters
Test: Test ()
2, 1. One-time judgment: if () {} else{}
2. Multiple judgments: if () {}
if () {}
if () {}
if () {}
3. Grade Judgment:
var a=99;
var b=math.floor (A/10);
Console.log ("B:", B)
Switch (b) {
Case 9:console.log ("A"); Break
Case 8:console.log ("B"); Break
Case 7:console.log ("C"); Break
Case 6:console.log ("D"); Break
Default:console.log ("fail"); Break
}
3. Loop: (1) do {
}while ()
(2) while () {i++}//i++ in the loop body, the statement in the loop body affects i++
(3) The i++ in the for (i=0;i<=100;i++) {}//for is outside the loop body, and the statements in the loop body do not affect the i++
continue;//end this cycle, continue the next time
break;//jump out of the loop body

4, Bubble sort: n number, will compare n-1 wheel, each round less than the previous comparison

Bubble sort (multiple loops)
var arr1=[2,4,1,3,6,9,78,0];
for (Var i=0;i<arr1.length-1;i++) {
for (Var j=0;j<arr1.length-1-i;j++) {
if (Arr1[j]>arr1[j+1]) {
var tmp=arr1[j];
ARR1[J]=ARR1[J+1];
arr1[j+1]=tmp;
}

}

}
Console.log (ARR1);
5. Data type
(1) var obj={a:100};
var fun1=function (obj) {
obj.a++;
}
FUN1 (obj);//a=101 reference type
(2) a=100;
var fun1=function (a) {
a++;
}//Primitive Type
FUN1 ()//a=100
(3) var obj={a:100};
var fun1=function (obj) {
var obj={a:100};
obj.a++;
}
FUN1 (obj);//a=100
IsNaN () to determine if it is Nan
6. Operators
1. Arithmetic operator: +-*/%
2. Comparison operator:> < >= <= = = = = = =
3, 0==false;1==true;
2==true; (Error)
3, Var B;
if (b) {
}//b when there is a value
if (! b) {
}//b when there is no value

It is generally not recommended to write null and Undefi
4, A++,++a,a+=b,a-=b


6. Example
Bubble sort
var arr1=[2,4,1,3,6,9,78,0];
for (Var i=0;i<arr1.length-1;i++) {
for (Var j=0;j<arr1.length-1-i;j++) {
if (Arr1[j]>arr1[j+1]) {
var tmp=arr1[j];
ARR1[J]=ARR1[J+1];
arr1[j+1]=tmp;
}

}

}
Console.log (ARR1);
Arr1.foreach (function (value) {
Console.log (Value,i);
});

Var in method
For (var k in arr1) {
Console.log (K,arr1[k]);
}
var obj={
Name: "Zhang Fei",
Sex: "Male",
Age:16
}
For (var m in obj) {
Console.log ("Var in method", M,obj[m]);
}

foreach method
var arr2=[1,2,3,4,50];
var arr3=[];
Arr2.foreach (function (value) {
value=value*10;
Console.log (value);
Arr3.push (value);
})
Console.log (ARR3);

Map () Method 1, Do loop 2, in the loop can return the new array
var arr4=arr2.map (function (value) {
if (value>10)
{return value*10;}
else{
return value;
}
})
Console.log (ARR4);

Filter () Method: Do the filtering
var arr5=arr2.filter (function (value) {
if (value>10)
{return value;}
})
Console.log ("ARR5:", ARR5);

JS Review 1

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.