Skip to content

Instantly share code, notes, and snippets.

@Grae-Drake
Created February 3, 2019 07:52
Show Gist options
  • Save Grae-Drake/2b6f94fa463d43e0ff87128749fff7e1 to your computer and use it in GitHub Desktop.
Save Grae-Drake/2b6f94fa463d43e0ff87128749fff7e1 to your computer and use it in GitHub Desktop.
// Boring.
function abbrevName(name) {
[first, last] = name.toUpperCase().split(' ');
return first[0] + '.' + last[0];
}
// Chain them method calls!
function abbrevName(name) {
return name.toUpperCase()
.split(' ')
.map(x => x[0])
.join('.');
}
// REDUCE.
// (Don't do this in real life.)
function abbrevName(name) {
return name.toUpperCase()
.split('')
.reduce((res, x, i) => res + (name[i - 1] === ' ' ? '.' + x[0] : ''));
}
# First, a very procedural approach.
def abbrevName(name):
first, last = name.upper().split(' ')
f = first[0]
l = last[0]
return f + '.' + l
# Now, a more compact one-liner. Less repetetion but also harder to read.
def abbrevName(name):
return '.'.join([x[0] for x in name.split(' ')]).upper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment