DEV Community

loading...

Discussion on: Coding Puzzles: Week of 4/8

Collapse
laurieontech profile image
Laurie • Edited

Quick edit with a dictionary.

function highlight(code) {
  const colorDict = {'F': 'pink', 'L': 'red', 'R':'green'};

  return code.replace(/(\D)\1*|\d+/g, (substr) => {
      let testChar = substr.charAt(0); 

      if (testChar in colorDict) {
        return '<span style="color: ' + colorDict[testChar] + '">' + substr + '</span>';   
      } else if (!isNaN(testChar)) {
        return '<span style="color: orange">' + substr + '</span>';   
      } else {
        return substr;
      }
  })
}