JavaScript fun: Help the postman classify addresses

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




Related Article

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.