Skip to content

07_reverseString: handle Unicode surrogate pairs in reverseString solution #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion 07_reverseString/solution/reverseString-solution.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const reverseString = function (string) {
return string.split("").reverse().join("");
return Array.from(string).reverse().join("");

/*
The following statement: `return string.split("").reverse().join("");`
also works but only if the passed string doesn't include any complex Unicode character,
i.e. a characher stored in a surrogate pair, which is a pair of 16-bit code units that represents a single character.
Example: emojis as in "Great 👍" and some accented characters as in "mañana".

The `split()` method splits the String by UTF-16 code units and will separate surrogate pairs resulting in invalid characters.

Search about 'Surrogate Pairs' and 'Grapheme Clusters' for more information.
*/
};

module.exports = reverseString;