DEV Community

loading...

Discussion on: Coding Puzzles: Week of 4/8

Collapse
clandau profile image
Courtney

The answers to this kata on codewars are blowing my mind.

function highlight(code) {
    let codeString = '';
    let leftPointer, currentItem;
    let colorObj = {'F': '<span style=\"color: pink\">', 'L': '<span style=\"color: red\">', 'R': '<span style=\"color: green\">'};
    for(let i=0; i<code.length; i++){
        currentItem = code[i];
        if(!isNaN(code[i])) {
            codeString += '<span style=\"color: orange\">'
            leftPointer = i;
            while(i+1 < code.length && !isNaN(code[i+1])) {
                i++;
            }
            codeString += code.slice(leftPointer, i+1) + '</span>';
        }
        else if(colorObj[currentItem]) {
            codeString += colorObj[currentItem];
            while(i+1 < code.length && currentItem === code[i+1]) {
                i++;
                codeString += currentItem;
            }
        codeString += currentItem  + '</span>';
        }
        if(currentItem === '(' || currentItem === ')') codeString += currentItem;
    }
    return codeString;
  }