http://www.jianshu.com/p/988840419605
Related knowledge points
Data types, operations, objects, function, inheritance, closures, scopes, prototype chains, events, REGEXP, JSON, Ajax, DOM, BOM, memory leaks, Cross-domain, asynchronous loading, template engine, front-end MVC, front-end MVVM, routing, modularity, Http, Canvas, JQuery, ECMAScript 2015 (ES6), Node.js, Angularjs, Vue, react ... Title & Answer function memory, judge is not prime.
Method One:
function isPrime1 (n) {
if (n<=3) {return true}
else{for
(var i=2;i<math.sqrt (n); i++) {
if (n%i==0) {return false;}
}
return true;
}
;
Method Two: Hash
var isprime2= (function () {//hash
var hash={};
return function (n) {
if (n<=3) {return true}
else if (hash[n]!==undefined) {return
hash[n];
} else{for
(var i=2;i<math.sqrt (n), i++) {
if (n%i==0) {return hash[n]=false}
} return
hash[n]= true;
}
}
) ();
Array to weight
method one: Var arr1=[1,2,3,2,1,2]; function Repeat1 (arr) {for (Var i=0,arr2=[];i<arr.length;i++) {if (Arr2.indexof (arr[i)) ==-1) {Arr2.push
(Arr[i]);
}}//(traversal end) return arr2; Method Two: hash function repeat2 (arr) {//traverses each element in the ARR and declares that the hash for (Var i=0,hash={};i<arr.length;i++) {//hash contains
The current element value is built//if not included, a new element is added to the hash, with the current element value Key,value defaults to 1 if (hash[arr[i]]===undefined) {hash[arr[i]]=1;
}}//(traversal end)///Convert hash to index: Var i=0;
var arr2=[];
For (arr2[i++] in hash);
return arr2; Method Three: Function Repeat3 (arr) {return Arr.sort (). Join (",,"). Replace (/(^|,,) (
[^,]+) (,, \2) */g, "$1$2"). Split (",,"); } console.log (REPEAT3 (arr1));
Insert Sort
var arr=[2,4,1,5,3];
function Insertsort (arr) {
//traverse arr each element (I starting from 1)
for (Var i=1;i<arr.length;i++) {
//To temporarily save the current element in the variable t
var t=arr[i];
var p=i-1;//declares variable p=i-1
//Loop: (arr[p]>t&&p>=0) {while
(arr[p]>t&&p>=0) {
// Assigning the value of P position to p+1 position
arr[p+1]=arr[p];
p--;//p--
}//(Loop end)
arr[p+1]=t;//Place T in p+1 position
}//(Loop End)
}
Insertsort (arr);
Console.log (String (arr));
Quick sort:
function QuickSort (arr) {
//if arr length<=1 if
(arr.length<=1) {
return arr;//directly returns arr
}
Compute the reference bit P
var p=math.floor (ARR.LENGTH/2);
var left=[];
var right=[];
Deletes the element of P position
var center=arr.splice (p,1) [0];
Iterate through the arr for each element for
(var i=0;i<arr.length;i++) {
if (arr[i]>=center) {
Right.push (arr[i));
} else{
Left.push (Arr[i]);
}
Return QuickSort (left). Concat (Center,quicksort)
}
var sortedarr=quicksort (arr);
Console.log (String (Sortedarr));
Regular expressions
(1) "Ryan5 is6 not7 A8 good9 man10"
var n=5;
var str= "Ryan is not a good mans";
Str=str.replace (/\b[a-z]+\b/g,function (kw) {return kw+n++;});
Console.log (str);
What is the number of occurrences of each character in the statistics string? The time of the show?
var str= "HelloWorld";
Method One: Hash for
(var i=0,hash={};i<str.length;i++) {
if (Hash[str[i]]) {
hash[str[i]]++
}else{
hash[str[i]]=1
}
}
Console.dir (hash);
Method Two: Use the regular
var arr=str.split ("")
. Sort ().
join ("")
. Match ([[A-z]) \1*/g
. Sort (function (a) , b) {return
b.length-a.length;})
Console.log ("appears most is:" +arr[0][0]
+ "altogether" +arr[0].length+ "the Time");
var hash={};
Arr.foreach (function (val) {
hash[val[0]]=val.length;
});
Console.dir (hash);
Array Dimension Reduction
var arr=[
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
];
Method 1: For
(var r=0,arr1=[];r<arr.length;r++) {for
(var c=0;c<arr[r].length;c++) {
Arr1.push ( Arr[r][c]);
}
Console.dir (arr1);
Method 2: For
(var r=0,arr2=[];r<arr.length;r++) {
arr2=arr2.concat (arr[r]);
}
Console.dir (ARR2);
Method 3:
var arr2=[].concat.apply ([],arr);
Console.dir (ARR2);
function Assignment
var f=function () {var a=b=1;}
f ();
Console.log (b);//1
Console.log (a);/error
var f=function () {var a=b=1;}
SetTimeout (f,0);
Console.log (b);//Error
F ();
var a,b=0, fn=function () {var a=b=2;}
fn ();
Console.log (a);//undefined
Console.log (b);//2
function currying (Coliward)
var getn;
function Add (n) {
getn=function () {console.log (n);}
return function (m) {
n+=m;
Arguments.callee.tostring=function () {return
n;
}
Return Arguments.callee
}
}
Add (1) (2) (3); GETN ();//6
Add (1) (2) (3) (4); Getn ();//10
alert (Add (1) (2) (3));//6
alert (Add (1) (2) (3) (4));//10
Recursion
var emp={
work:function () {//3,2,1
var sum=0;//+3+2+1 +2+1 +1 for
(vari=0; i< arguments.length&&arguments[0]>0;
i++) {
sum+=arguments[i]
+arguments.callee (
--arguments[i]
);
}
return sum;
}
Console.log (Emp.work (3,2,1));//10
Closed Bag
(1) function Fun (n,o) {//outer functions Console.log (o);
return {fun:function (m) {//inner-layer function n return fun (m,n); }} var a=fun (0); A.fun (1); A.fun (2);
A.fun (3);
Undefined 0 0 0 var a=fun (0). Fun (1). Fun (2). Fun (3);