Skip to content

Instantly share code, notes, and snippets.

@3AHAT0P
Last active April 12, 2019 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3AHAT0P/900d7635c16974f73455f94d69252134 to your computer and use it in GitHub Desktop.
Save 3AHAT0P/900d7635c16974f73455f94d69252134 to your computer and use it in GitHub Desktop.
Reverse string in JS
const isSupportSymbol = (symbolCode) => symbolCode === 55356 || (symbolCode >= 768 && symbolCode <= 879);
const reverse = (str) => {
let res = '';
let prevSymbol = null;
for (const symbol of str) {
if (isSupportSymbol(symbol.charCodeAt())) {
res = `${prevSymbol}${symbol}${res}`;
prevSymbol = null;
} else if (prevSymbol == null) {
prevSymbol = symbol;
} else {
res = `${prevSymbol}${res}`;
prevSymbol = symbol;
}
}
return res;
}
// test
reverse("πŸ˜€πŸ‘‰πŸΏΠ•ΜˆπŸ‘ƒπŸ½"); // "πŸ‘ƒπŸ½Π•ΜˆπŸ‘‰πŸΏπŸ˜€"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment