JS Built-ins
← String167 / 195
StringString.prototype.toWellFormedintermediate

String.prototype.toWellFormed() β€” lone surrogate replaced with U+FFFD

πŸ“– Usage Example
'hello'.toWellFormed()      // β†’ 'hello'
'πŸ˜€'.toWellFormed()         // β†’ 'πŸ˜€'
'οΏ½'.toWellFormed()     // β†’ 'οΏ½'

Use toWellFormed() to replace lone surrogate code units with the Unicode replacement character U+FFFD (ES2024).

String.prototype.toWellFormed() β€” Lone Surrogate

A lone high surrogate like '\uD800' is not a valid Unicode scalar. .toWellFormed() replaces it with '\uFFFD'.

Challenge: Implement fixLoneSurrogate() that converts '\uD800' to a well-formed string.

fixLoneSurrogate() // β†’ 'οΏ½'
πŸ’‘ Hints (2)
  • β€’ `\uD800` is a lone high surrogate β€” it has no paired low surrogate.
  • β€’ `toWellFormed()` replaces it with `\uFFFD` (the Unicode replacement character).