Front-end siege lions learning Note 3: implements a URI parsing method, which parses the # parameters in the URL into the specified data structure.

Source: Internet
Author: User
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.

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.