Last active
May 14, 2018 17:55
-
-
Save ivankahl/7491724 to your computer and use it in GitHub Desktop.
This function returns the initials of a name which is passed to it.
This file contains 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
function(fn) | |
{ | |
var n=fn.split(" "), // Split the full name into an array | |
it="", // Create and set the intials variable to nothing | |
i; // Set i to 0 for the while loop | |
while(i<n.length) // While loop that runs while i is smaller than the length of the array of names | |
{ | |
it+=n[i][0]; // Get the first letter from the name and add it to the it(initial) variable | |
i++ // Increment i by 1 | |
} | |
return(it.toUpperCase()) // Convert the initials to uppercase and then return it | |
} |
This file contains 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
function(fn){var n=fn.split(" "),it="",i;while(i<n.length){it+=n[i][.substr(0,1)[0];i++}return(it.toUpperCase())} |
//function(n){for(n=n.split(" "),t="",r=0;r in n;)t+=n[r][0],r++;return t.toUpperCase()}
//86 bytes
alert(function(n){for(n=n.split(" "),t="",r=0;r in n;)t+=n[r][0],r++;return t.toUpperCase()}('Dave Brown'))
I like periods after them:
//function(n){for(n=n.split(" "),t="",r=0;r in n;)t+=n[r][0]+'. ',r++;return t.toUpperCase()
//90 bytes
alert(function(n){for(n=n.split(" "),t="",r=0;r in n;)t+=n[r][0]+'. ',r++;return t.toUpperCase()}('Dave Brown'))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just shortened it some more!