CodeNot attempted
escapeHtml
Difficulty: easy
Write escapeHtml(text) so the result is safe to put inside HTML or a quoted attribute. Replace & < > " ' with & < > " '. Make sure nothing gets escaped twice.
Show reference solution
const replacements: Record<string, string> = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''',};
export function escapeHtml(text: string): string { let result = ''; for (const char of text) { // Swap the five special characters, keep everything else as it is. result += replacements[char] ?? char; } return result;}
Ctrl Enter