[JS Practice] Filter invalid login

Title :

Filter invalid login

Description:

While developing a website, you detect that some of the members have troubles logging in. Searching through the code you find that all logins ending with a “_” make problems. So you want to write a function that takes an array of pairs of login-names and e-mails, and outputs an array of all login-name, e-mails-pairs from the login-names that end with “_”.

If you have the input-array:

[ [ "foo", "foo@foo.com" ], [ "bar_", "bar@bar.com" ] ]

it should output

[ [ "bar_", "bar@bar.com" ] ]

My solution :

function searchNames( logins ){
    return logins.filter(function(value, index) {
        if(value[0].lastIndexOf("_") == value[0].length-1) {
            return true;
        }
        return false;        
    });
}

A solution with only one line :

function searchNames( logins ){
  return logins.filter(function (login) { return login[0].slice(-1) === '_' });
}

Ref. from codewars

Advertisements

2 comments on “[JS Practice] Filter invalid login

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s