Convert HTML Entities failing 1 test

Convert HTML Entities failing 1 test
0

#1

Tell us what’s happening:
Hello, my code passes all the tests except one, which is why I find this so strange. It fails on the convertHTML("<>") should return &​lt;&​gt; test, and yet it successfully converts those same characters on the other tests, so I can’t figure out why it fails on this one.

Your code so far


function convertHTML(str) {
  let testChar = '&';
  console.log('testChar: ' +testChar);
  let newChar = '';
  let regex = /[&<>"']/g
  let finalStr = '';

  function convert(obj) {
    let converted = '';
    switch(obj) {
      case '&':
        converted = '&amp;';
        break;
      case '<':
        converted = '&lt;';
        break;
      case '>':
        converted = '&gt;';
        break;
      case '"':
        converted = '&quot;';
        break;
      case "'":
        converted = '&apos;';
        break;
    }
    console.log('converted: ' +converted);
    return converted;
  }

  let strArr = str.split('');
  console.log(strArr);

  for (let i = 0; i < strArr.length; i++) {
    if (regex.test(strArr[i])) {
      newChar = convert(strArr[i]);
      strArr.splice(i,1,newChar);
      console.log('splice: ' +strArr);
    }
  }
  finalStr = strArr.join('');
  console.log(finalStr);
  // &colon;&rpar;
  return finalStr;
}

convertHTML("<>");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
Won’t let me post links with this new account so had to delete the link from the template.


#2

The issue is that for some reason regex.test(strArr[i]) is not making the if statement execute for the second element

Catched!

See specifically the part under Examples > Using test() on a regex with the global flag


#3

Removing the global flag did indeed fix it, thanks!