-
Notifications
You must be signed in to change notification settings - Fork 0
/
49-js-filter-3.js
21 lines (20 loc) · 1.36 KB
/
49-js-filter-3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Your friend saw the great work you did with keeping your user-names at bay in
// http://www.codewars.com/dojo/katas/525d9b1a037b7a9da7000905
// now he'd like you to do (nearly) the same thing for his website, but as always,
// the devil is in the details.
// He has troubles with users ending or starting in a ".", and his user-array is
// a flat array of user-email-pairs, like so:
// He is only interested in e-mailing the users and ask them to sign up again, so
// no need to keep the user-name, only e-mail addresses for the user-names that start
// or end with a "." should be returned. For the above array, the correct return-array would be
// [ "[email protected]" ]
// You have to use the filter-method of Javascript, which returns each element of the array for
// which the filter-method returns true. The second argument gives the index you're looking at
// and the third argument is the array itself:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
export default (logins) => {
const withDot = logins.filter(e => e[0] === '.' || e[e.length - 1] === '.').map(e => (e[0] === '.' ? e.slice(1) : e.slice(0, -1)));
const emails = logins.filter(e => e.includes('@'));
return emails.filter(e => !!withDot.find(el => e.slice(0, e.indexOf('@')).includes(el)));
};