Front-End written questions: Beat the multidimensional array, extract the parameters in the URL into an object, implement the trim () function of a string, determine the email address, 16 binary color to RGB format

Source: Internet
Author: User
Tags ustring

(1) Flat array

This is a well-known internet company today's front-end written questions: multidimensional array dimension variable, such as [2,4,1,[2,3,4,[1,2],5,3],3,8] beat flat for [2,4,1,2,3,4,1,2,5,3,3,8]. The number of dimensions is variable and should be recursive:

Answer:
function flatten(arr){var arrLength=arr.length;fori=0;i<arrLength;i++){    if(arr[i].constructor==Array){        arr.splice(i,1,flatten(arr[i]));    }}return arr;}
(2) Extract the parameters from the URL into an object

This is also an internet company's front-end pen test, with Memory records,
A URL such as "http://www.taobao.com/index.php?key0=0&key1=1&key2=2 ..." Extract parameters, Output:
{
key0:0,
Key1:1,
Key2:2,
...
}

Answer:
function  parsequerystring   (ustring)  { ustring= Ustring.slice (Ustring.indexof () +1 ); var  arr=ustring.split ( "&" ); var  result=new   Object  (); for  (var  i=0 ; i<arr.length;i++) {var  kv=arr[i]; Kvarray=kv.split ( "=" ); Result[kvarray[0 ]]=kvarray[1 ]; } return  result} 
(3) The use of JS code to achieve the removal before and after the string before and after the white space trim () function

Line such as "abc" = = "abc" "ABC" ==> "ABC"

Answer:
    String.prototype.trim=function(){       var result=this.replace(/^\s+|\s+$/g,"");        return result;    }
(4) Implement a function to determine if it is the correct email address

The problem is the use of Kauzheng expression, email address such as [email protected]
So the notation should be/^ ([a-za-z0-9_.-]) +\@ ([a-za-z0-9-]) +.) + ([a-za-z0-9]{2,4}) $/

Answer:
function checkEmailAddress(address){    if(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/.test(address)){        returntrue    }else{        returnfalse    }}
(5) 16 binary colors to RGB format: requirements: #0000ff = = RGB (0,0,255)

Invalid=>invalid
#G00 ==> #G00

Answer:
varParsecolor = function(val){      varR, G, B;//parameter is RGB mode, do not make a binary conversion, directly intercept the string can     if(/rgb/. Test (val)) {vararr = Val.match (/\d+/g); R =parseint(arr[0] ); g =parseint(arr[1] ); b =parseint(arr[2] ); }///parameter is 16 binary requires a binary conversion     Else if(/#/. Test (val)) {varlen = val.length;//Non-abbreviated mode #0066cc         if(len = =7) {r =parseint(Val.slice (1,3), -); g =parseint(Val.slice (3,5), -); b =parseint(Val.slice (5), -); }Else if(len==4){returnVal }     }Else{returnVal }return the RGB ("+r+","+g+","+b+")"; };

Front-End written questions: Beat the multidimensional array, extract the parameters in the URL into an object, implement the trim () function of a string, determine the email address, 16 binary color to RGB format

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.