settingsAccountsettings
Menusettings

Q: Usernames - parse a list of emails and return a list of usernames

+2 votes

Write a JavaScript function that parses a list of emails and returns a list of usernames, generated from them. Each username is composed from the name of the email address, a period and the first letter of every element in the domain name. See the examples for more information.

The input comes as array of string elements. Each element is an email address.

Examples:

Input:
['peshoo@gmail.com', 'todor_43@mail.dir.bg', 'foo@bar.com']

Output:
peshoo.gc, todor_43.mdb, foo.bc

The output is printed on the console on a single line as a comma-formatted list.

asked in JavaScript category by user mitko

1 Answer

+1 vote

The js solution:

function username(arrStr) {
    let result = [];

    for (let email of arrStr) {
        let [alias,domain]=email.split("@");//using this solution because we know how many elements (2) are in the e-mail: (1) before @ and (2) after @
        let username = alias + ".";
        let domainParts = domain.split(".");
        domainParts.forEach(p=>username += p[0]);
        result.push(username);
    }
    console.log(result.join(", "));
}

username(['peshoo@gmail.com', 'todor_43@mail.dir.bg', 'foo@bar.com']);

 

answered by user nikole
...