The following is my implementation. In particular, if the zip code format is incorrect (not 8 bits), you do not need to perform string operations and directly return the result. A postman is about to send an email. He has a list of customer addresses, as shown below:
"123 Main Street St. Louisville OH 43071,432 Main Long Road St. Louisville OH 43071,786 High Street pollockville NY 56432 ".
The preceding example contains three addresses separated by commas.
For example,"123 Main Street St. Louisville OH 43071"The address is actually in this format.
"Street number, street name, town name, zip code"The zip code is as follows:& Quot; OH 43071 & quot"It is a fixed 8-digit, the first two digits are uppercase letters, followed by a space, and finally five digits.
The postman wantsZip code.
Then the task comes. Write a function and input two parameters.
Travel (list, zipCode)
List indicates the address list, zipCode indicates the zip code, and the returned value is a string in the following format:
"Zip code: street name and town name, street name and town name,.../house number, house number ,..."
If the given ZIP Code cannot be found in the list, the following is returned:
"Zip code :/"
The preceding address list is used as an example:
r = "123 Main Street St. Louisville OH 43071,432 Main Long Road St. Louisville OH 43071,786 High Street Pollocksville NY 56432"travel(r, "OH 43071") \\ "OH 43071:Main Street St. Louisville,Main Long Road St. Louisville/123,432"travel(r, "NY 56432") \\ "NY 56432:High Street Pollocksville/786"travel(r, "NY 5643") \\ "NY 5643:/"
The idea of this question is very simple. We examine how to operate strings. It is better to use regular expressions without using regular expressions.
The following is my implementation. In particular, if the zip code format is incorrect (not 8 bits), you do not need to perform string operations and directly return the result.
function travel(r, zipcode) { var addresses = r.split(","); var result = zipcode + ":"; var houseNumber = []; var streets = []; if(zipcode.length == 8){ for(var i=0;i= 0){ var firstBlank = current.indexOf(" "); houseNumber.push(current.slice(0,firstBlank)); streets.push(current.slice(firstBlank + 1,pos - 1)); } } } return result + streets.join(",") + "/" + houseNumber.join(",");}
The above is the JavaScript interesting question: Help the postman classify the content of the address. For more information, please follow the PHP Chinese Network (www.php1.cn )!