Your challenge today is to initialize an individual's middle name (if there is any).
Example:
'Jack Ryan' => 'Jack Ryan'
'Lois Mary Lane' => 'Lois M. Lane'
'Dimitri' => 'Dimitri'
'Alice Betty Catherine Davis' => 'Alice B. C. Davis'
Good luck!
This challenge comes from manonacodingmission on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (20)
CSS
Wrap each word in a
span
, and add the class "mn" to the container.Live demo on CodePen.
I also did a JavaScript version on that same demo:
If you select-and-copy the names, the full name is copied instead of the shortened one.
Not the best solution: ))
Ruby solution
I just changed a few things in a similar function from singl.page. Doesn’t use join(). Works when only a single name is provided.
PHP Solution
Here's mine, with a more functional approach - also takes into account name strings that have erroneous spaces
jsbin.com/gedejih/edit?js,console
Javascript
This was fun :)
Python
JavaScript: