nodejh
10/24/2016 - 12:00 PM

convert_to_roman.js

convert_to_roman.js

function convert(num) {
 var s = {
    1:'I',
    2: 'II',
    3: 'III',
    4: 'IV',
    5: 'V',
    6: 'VI',
    7: 'VII',
    8: 'VIII',
    9: 'IX',							
    10: 'X',
    20: 'XX',
    30: 'XXX',
    40: 'XL',
    50: 'L',
    60: 'LX',
    70: 'LXX',
    80: 'LXXX',
    90: 'XC',					
    100: 'C',
    200: 'CC',
    300: 'CCC',
    400: 'CD',
    500: 'D',
    600: 'DC',
    700: 'DCC',
    800: 'DCCC',
    900: 'CM',
    1000: 'M',
    2000: 'MM',
    3000: 'MMM'
};
  var a = parseInt(num / 1000) * 1000;
  var b = parseInt( (num % 1000) / 100) * 100;
  var c = parseInt( (num % 100) / 10) * 10;
  var d = parseInt( (num % 10) / 1) * 1;
  var res = '';
  if (a > 0) {
    res += s[a];
  }
  if (b > 0) {
    res += s[b];
  }
  if (c > 0) {
    res += s[c];
  }
  if (d > 0) {
    res += s[d];
  }
 return res;
}

convert(36);