Hello worldπ©βπ¦°π©βπ©βπ¦βπ¦
π©βπ©βπ¦βπ¦π©βπ¦°dlrow olleH
I tried several approaches but none gave me correct answer.
This failed miserablly:
const text = 'Hello worldπ©βπ¦°π©βπ©βπ¦βπ¦';
const reversed = text.split('').reverse().join('');
console.log(reversed);
This kinda works but it breaks π©βπ©βπ¦βπ¦
into 4 different emojis:
const text = 'Hello worldπ©βπ¦°π©βπ©βπ¦βπ¦';
const reversed = [...text].reverse().join('');
console.log(reversed);
I also tried every answer in this question but none of them works.
Is there a way to get the desired output?
Strings can be reversed using slicing. To reverse a string, we simply create a slice that starts with the length of the string, and ends at index 0. The slice statement means start at string length, end at position 0, move with the step -1 (or one step backward).
innerHTML = String. prototype. reverse(s); after the element was loaded in page, so the value of s was empty (since it just loaded), and that function was never being called again to readjust the inner HTML of the p tag. Hope that can help.
If you're able to, use the _.split()
function provided by lodash. From version 4.0 onwards, _.split()
is capable of splitting unicode emojis.
Using the native .reverse().join('')
to reverse the 'characters' should work just fine with emojis containing zero-width joiners
function reverse(txt) { return _.split(txt, '').reverse().join(''); }
const text = 'Hello worldπ©βπ¦°π©βπ©βπ¦βπ¦';
console.log(reverse(text));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With