convertHTML

Medium · String Manipulations · 2 test cases

Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.

Examples

convertHTML('Dolce & Gabbana') → 'Dolce &amp; Gabbana'

Starter code

function convertHTML(str) {

}

Complexity of the optimal solution

Time O(n), space O(n). The `replace` method with a global regex will scan the entire string of length n. A new string is built, which can have a length proportional to n. Both time and space are linear.

Solve convertHTML in the browser