JavaScript Algorithms and Data Structures Projects: Roman Numeral

function convertToRoman(num) {
    return num.toString()
        .split('').reverse()
        .map((num, index) => roman_number_map[index][num])
        .reverse()
        .join('')
};
var roman_number_map = {
    0: {
        1: 'I',
        2: 'II',
        3: 'III',
        4: 'IV',
        5: 'V',
        6: 'VI',
        7: 'VII',
        8: 'VIII',
        9: 'IX',
    },
    1: {
        1: 'X',
        2: 'XX',
        3: 'XXX',
        4: 'XL',
        5: 'L',
        6: 'LX',
        7: 'LXX',
        8: 'LXXX',
        9: 'XC',
    },
    2: {
        1: 'C',
        2: 'CC',
        3: 'CCC',
        4: 'CD',
        5: 'D',
        6: 'DC',
        7: 'DCC',
        8: 'DCCC',
        9: 'CM',
    },
    3: {
        1: 'M',
        2: 'MM',
        3: 'MMM',
    },
};

console.log(convertToRoman(1223));
1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

1 Like