In today's challenge, we ask that you to write a function that returns a string in which an individual's first name is swapped with their last name.
Example: nameShuffler('Emma McClane'); => "McClane Emma"
Good luck!
This challenge comes from debri 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!
Latest comments (25)
JavaScript:
Here is the simple solution with Python:
My solution in js
Seems pretty simple (Haskell)
Python :
Buffer goes buf-buf
Nice and simple python implementation 😁
There is my python solution
Rust Playground
Ruby!
Just because I'm lazy
There are a few answers in JavaScript already, so let's try some things in CSS 🤩
We could separate the words into
<span>
and, using Flexbox, change the order in which they are displayed within the parent container using theorder
property. It can be easily expanded to more than 2 words, but it requires that wrapping to work.Without having to add any additional tags, we can use data-attributes to have the name and last name, and use the
::before
and::after
to display them in inverted order. It is really simple to implement but not too extendable:Also without having any additional tags, we can play with multiple text-shadows and positioning to replicate the words in inverted order. For this solution, you need to know the size of each word, and it doesn't work for more than two words... still it is a cool approach too.
You can see a demo of all of them on CodePen:
Oh damn! That box Shadow solution is pretty damn impressive!
I think I may have gone overboard with the ol' backslashes for escaping, but...
ruby 2.7