Last active
April 12, 2019 10:57
-
-
Save 3AHAT0P/900d7635c16974f73455f94d69252134 to your computer and use it in GitHub Desktop.
Reverse string in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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