Skip to content

Instantly share code, notes, and snippets.

@ivankahl
Last active May 14, 2018 17:55
Show Gist options
  • Save ivankahl/7491724 to your computer and use it in GitHub Desktop.
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.
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
}
function(fn){var n=fn.split(" "),it="",i;while(i<n.length){it+=n[i][.substr(0,1)[0];i++}return(it.toUpperCase())}
@ivankahl
Copy link
Author

Just shortened it some more!

@JavaScript-Packer
Copy link

//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'))

@JavaScript-Packer
Copy link

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