Interview Questions
This is an interview with Sohu Javascript. The requirements are as follows:
Implements a URI parsing method to resolve the # parameter in the URL to the specified data structure.
Function Urlparser (s ){ // ++ Answer area ++ // ++ } Try { VaR Url1 = "http://www.abc.com/m/s/#page/2? Type = latest_videos & page_size = 20" ; VaR Url2 = "http://www.abc.com/m/s/#type=latest_videos&page_size=20" ; VaR Url3 = "http://www.abc.com/m/s/#page? Type = latest_videos & page_size = 20" ; Console. Group (); console.info (urlparser (url1); console.info (urlparser (url2); console.info (urlparser (url3); console. groupend (); /* ------ [Execution result] ------ ["page", "2", {"type": "latest_videos", "page_size": 20}] [{"type ": "latest_videos", "page_size": 20}] ["page", {"type": "latest_videos", "page_size": 20}] ---------------- */ } Catch (E) {console. Error ( "Execution error, error message:" + E );}
Analysis Process
This question is based on the test, and there is no good analysis.CodeRight.
Function Urlparser (s ){ // ++ Answer area ++ VaR Marr = []; VaR Parr = []; VaR M = S. Split ("#") [1], P = "" ; If (M. indexof ("? ")>-1 ){ VaR Temp = M. Split ("? ") [0 ]; P = M. Split ("? ") [1 ]; Marr = Temp. Split ("/" ); If (Marr [Marr. Length-1] = "" ) {Marr. Pop ();}} Else {P = M;} Parr = P. Split ("&" ); VaR STR = "[" ; For ( VaR I = 0, L = Marr. length; I <L; I ++ ) {Str + = "\" "+ Marr [I] + "\"," ;} Str + = "{" ; For ( VaR I = 0, L = Parr. length; I <L; I ++ ) {Str + = "\" "+ Parr [I]. Split (" = "). Join (" \ ": \" ") + "\"," ;} Str + = "}]" ; Return Str. Replace (",}]", "}]" ); // ++ }
Summary
This question examines the basic knowledge of JavaScript, such as string segmentation and connection. It is a simple question. Of course, you can also write high-quality code for simple questions.