Reputation: 17
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.
Upvotes: 0
Views: 979
Reputation: 1
Another alternative:
function acronym(str) {
return str.toLowerCase().match(/(\b[a-z])/g).join('')
}
Though, .match
does not seem to be in the list of allowed methods
Upvotes: 3
Reputation: 141827
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
Upvotes: 1