Created
February 3, 2019 07:52
-
-
Save Grae-Drake/2b6f94fa463d43e0ff87128749fff7e1 to your computer and use it in GitHub Desktop.
Abbreviate a two word name: https://www.codewars.com/kata/abbreviate-a-two-word-name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] : '')); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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