Hi,
My solution passes all but one test case and that confuses me. The difference with other cases is that, in this case there is nothing in the string other than the chars that are being replaced but I don’t see why that should make a difference. Can anyone explain? The result I get for the test case is “undefined” It should be “<>:”.
My code is as follows:
function convertHTML(str) {
// :)
return str.replace(/[&<>"']+/g, htmlEncode);
function htmlEncode(match){
switch(match){
case "&":
return "&";
break;
case "<":
return "<";
break;
case ">":
return ">";
break;
case '"':
return """;
break;
case "'":
return "'";
break;
}
}
}
convertHTML("<>");
ps I realise the breaks are superfluous - just good habits to include them