immutable js


  • 0
    A
    const intToRoman = (num) => {
        const base = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
        const values = ["M","CM","D", "CD", "C","XC","L","XL", "X", "IX", "V", "IV", "I"];
        const iter = (acc, num, i) => {
          if (i === base.length) return acc;
          else if(num >= base[i]) return iter(acc + values[i], num - base[i], i);
          return iter(acc, num, i + 1);
        }
        return iter('', num, 0);
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.