Number to Roman numerals I've been working on numerals conversion lately. Now the JavaScript course I'm following asked me to do something similar for Roman numerals.
To keep things fresh and given the amount of test-cases provided I decided to write this one Test-Driven Development style. That is, for as much as I understand it.
First I check whether it's dividable by a full numeral (M, C, etc.). After that I have to reverse the string because the numerals were added in reverse order. The next step is to translate all combinations into their correct form using regex and a translation map. The functions were written in that order and fixing the problem the last part caused.
So far it looks very messy. Is this how one is supposed to solve problems TDD style? There is definitely a lot of code duplication going on, could this be fixed with another map?
Challenge

Convert the given number into a Roman numeral.
All Roman numerals answers should be provided in upper-case.

Test cases
convert(5) // &quot;V&quot;.
convert(9) // &quot;IX&quot;.
convert(12) // &quot;XII&quot;.
convert(16) // &quot;XVI&quot;.
convert(29) // &quot;XXIX&quot;.
convert(44) // &quot;XLIV&quot;.
convert(45) // &quot;XLV&quot;
convert(68) // &quot;LXVIII&quot;
convert(83) // &quot;LXXXIII&quot;
convert(97) // &quot;XCVII&quot;
convert(99) // &quot;XCIX&quot;
convert(500) // &quot;D&quot;
convert(501) // &quot;DI&quot;
convert(649) // &quot;DCXLIX&quot;
convert(798) // &quot;DCCXCVIII&quot;
convert(891) // &quot;DCCCXCI&quot;
convert(1000) // &quot;M&quot;
convert(1004) // &quot;MIV&quot;
convert(1006) // &quot;MVI&quot;
convert(1023) // &quot;MXXIII&quot;
convert(2014) // &quot;MMXIV&quot;
convert(3999) // &quot;MMMCMXCIX&quot;

Code
function convert(num) {
  roman = &quot;&quot;;
  for (var i = 0; i &lt; num; i++) {
    //MDCLXVI
    if (!(num % 1000)) { roman += &quot;M&quot;; num -= 1000; }
    else if (!(num % 500)) { roman += &quot;D&quot;; num -= 500; }
    else if (!(num % 100)) { roman += &quot;C&quot;; num -= 100; }
    else if (!(num % 50)) { roman += &quot;L&quot;; num -= 50; }
    else if (!(num % 10)) { roman += &quot;X&quot;; num -= 10; }
    else if (!(num % 5)) { roman += &quot;V&quot;; num -= 5; }
    else if (!(num % 1)){ roman += &quot;I&quot;; num -= 1; }
  }
  roman = roman.split('').reverse().join('');
  var translationMap = {
    DCCCC : 'CM',
    CCCC : 'CD',
    LXXXX : 'XC',
    XXXX : 'XL',
    VIIII : 'IX',
    IIII : 'IV',
  };
  for (var i in translationMap) {
    roman = roman.replace(new RegExp(i,'g'), translationMap[i]);
  }
  return roman;
}

convert(3999);

Run it at repl.it
Roman letter to value map


if (!(num % 1000)) { roman += "M"; num -= 1000; }
else if (!(num % 500)) { roman += "D"; num -= 500; }
else if (!(num % 100)) { roman += "C"; num -= 100; }
else if (!(num % 50)) { roman += "L"; num -= 50; }
else if (!(num % 10)) { roman += "X"; num -= 10; }
else if (!(num % 5)) { roman += "V"; num -= 5; }
else if (!(num % 1)){ roman += "I"; num -= 1; }



You should be using a map here to store the roman numeral values and their corresponding characters:

var romanCharacterMap = {
    "M": 1000,
    "D": 500,
    ...
}


With this, you can easily add on more letters if you ever felt like expanding it. Now, here is what your code would look like there:

while (num &gt; 0) {
    for(var romanCharacter in romanCharacterMap) {
        var value = romanCharacterMap[romanCharacter];
        if(!(num % value)) {
            roman += romanCharacter;
            num -= value;
            break;
        }
    }
}


Then, to OOP this code up, you could move this function and the new map to an object to keep things together:

var RomanNumberalConverter = {
    ...
}


Then, the map won't be created every time the function is called; it will be created and referenced once.



Moar regexes... moar!

In this loop:


  for (var i in translationMap) {
    roman = roman.replace(new RegExp(i,'g'), translationMap[i]);
  }



Every iteration, you are creating a regex for every entry in the map. However, every time this function is called, the same regexes are created because that map is the same every time.

To speed things up, try creating the regex once and keeping it in the map that is also in this RomanNumberConverter object:

translationMap: {
    DCCCC: {
        replace: "CM",
        regex: /DCCCC/g
    },
    ...
}


Your code will now be faster because a new series of regexes doesn't have to be created every function call.