Nearly a week in the busy interview, the first time this month more Bo, very miss.
Basically big companies will require some algorithm or data structure class things, pick some interesting knock down. This aspect of oneself is not very proficient, can only step by step to accumulate.
1. According to the query string to get the object data, you can choose the format according to the requirements, which is generated in key:value format
//you can use regular matches here, but carefully backwards, some basic methods of string can solve the problem completely . varstr = ' WWW.TEST.COM/TEST?STR1=AA&STR2=BB&STR3=CC '; functionquerystr (str) {vartarget = Str.substr (Str.indexof ('? ') + 1); varGroupitem = Target.split (' & ')); vartemp = {} for(vari=0; i<groupitem.length; i++) { varKey = Groupitem[i].split (' = ') [0]; varValue = groupitem[i].split (' = ') [1]; Temp[key]=value; } returntemp; } console.log (Querystr (str))
2. Random permutation of the array, time complexity is very important!!
//ordinary random permutation can be, but in reality, the time-complexity of the algorithm is more important varArray = [1,5,9,6,2,6]; functionTemparray (array) {varLen =Array.Length; vartemp = []; while(len--) { varran = Math.floor (Math.random () *Len); Temp.push (Array.splice (ran,1)) [0]) } returntemp} console.log (Temparray (array))
3. Simulate the new process
Roughly divided into five parts:
(1) Create an empty object
(2) The __proto__ property of the object points to the prototype object of the constructor
(3) The constructor zodiac of the object is changed to a constructor function
(4) Correct the point of this pointer
(5) Return object
//constructor Function functionpeople (name, sex) { This. Name =name; This. Sex =sex; This. Eat =function() {Console.log ( This. Name + ' eat ') } } functionnewpeople (name, sex) {//Create an empty object varobj = {} //specifying the __proto__ propertyobj.__proto__ =People.prototype; //Specifying ConstructorsObj.constructor =people; //This pointer correctspeople.call (obj, name, sex); //return Object returnobj; } varP1 =NewPeople (' Kevin ', ' Male ') varP2 = newpeople (' Jenny ', ' female ') Console.log (p1) p1.eat () Console.log (p2) p2.eat ()
4. Native JS implementation of custom events, object-oriented thinking implementation
//Custom event Classes functionMyEvent () { This. evlist = {} This. addevent =function(Type, fn) {if(typeoffn!== ' function ')return This. evlist[type] =fn} This. dispatchevent =function(type, data) {varDataobj ={} dataobj.data=Data This. Evlist[type] (dataobj)}} varEV =NewMyEvent ()//Adding custom eventsEv.addevent (' Test ',function(e) {alert (E.data)})//Triggering custom EventsDocument.queryselector ('. btn '). onclick =function() {ev.dispatchevent (' Test ', ' pomelo ') }
5. Array of four ways to weight, again, time complexity is important!!
vararr = [1,1,2,3,5,5] //compare from front to back, same deleteArray.prototype.distinct1 =function () { vararr = This; for(vari=0; i<arr.length; i++) { for(varj=i+1; j<arr.length; J + +) { if(Arr[i] = = =Arr[j]) {Arr.splice (J,1) J= ++i; } } } returnarr}//previous backward comparison, different push to new arrayArray.prototype.distinct2 =function () { vararr = This; vartemp = []; for(vari=0; i<arr.length; i++) { for(varj=i+1; j<arr.length; J + +) { if(Arr[i] = = =Arr[j]) {J= ++i; }} temp.push (Arr[i])}returnTemp}//attributes that cannot be duplicated by using object property namesARRAY.PROTOTYPE.DISTINCT3 =function () { varobj = {}; vararr = This; vartemp = []; for(vari=0; i<arr.length; i++) { if(!Obj[arr[i]]) {Temp.push (Arr[i]) obj[arr[i]= 1; } } returnTemp}//using indexof if it cannot match to the same, push into the new arrayArray.prototype.distinct4 =function () { vararr = This; vartemp = []; Arr.foreach (function(item, i) {varnum = Arr.indexof (item, i+1) if(num = =-1) {Temp.push (item)}})returntemp} console.log (Arr.distinct1 ()) Console.log (Arr.distinct2 ()) Console.log (arr.distinct 3 ()) Console.log (Arr.distinct4 () )
Algorithms and JS techniques encountered in interview questions (I.)