Anthony Boyac Anthony Boyac - 1 year ago 114
Javascript Question

Acronym function without loop on javascript

I need to write an acronym function that yields the acronym of whatever I put as my value. I cannot use a loop, but I can use map, reduce, or filter as well as join and split. I have been able to get the acronym of an array, but that's not what I am supposed to do. This should work when trying a value to get its acronym--> acronym(I know, right?) this turns to "ikr."

You don't need to give me the code; I want to know what I can use that is not a loop to get this code to work.

Answer Source
function acronymn( str ) {
    return str.split( /\b(?=[a-z])/ig ) // split on word boundaries
      .map( token => token[0] )         // get first letter of each token
      .join( '' ).toLowerCase()         // convert to lowercase string

acronymn( 'I know, right?' ) === 'ikr' // true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download