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!
Oldest comments (25)
You mean like this? It would work for your use case, although I don't know how you want to handle things like having more than 2 names or last names?
And with arrow function
Yeap, a quick, little fun one liner. I love it!
It may not work if more than 2 names are provided, but hey:
probably a terrible practice but that's how I'd do it
Python version using static typing features.
Ruby Language
Robust version and with test specs!
output
ruby 2.7
I think I may have gone overboard with the ol' backslashes for escaping, but...
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!
Just because I'm lazy
Ruby!
Rust Playground
There is my python solution
Nice and simple python implementation 😁
Buffer goes buf-buf