Roman Numeral Converter Program

Can anyone suggest ways to improve this code?

function convertToRoman(num) {
  let one = ["M", "C", "X", "I"] // [1000, 100, 10, 1]
  let six = [0, "DC", "LX", "VI"] // [6000, 600, 60, 6]
  var romanNumerals = {
    // Each value contains a list with the roman numerals in the Thousands, Hundreds, Tens, Ones order  
    // for that number
    "0": ["", "", "", ""], // Default value 0 stores empty string
    "1": one,
    "2": one.map(i => i += i[i.length -1]), //Adds repeated value (M, C, X, I) [2000, 200, 20, 2]
    "3": one.map(i => i += i[i.length -1].repeat(2)),
    // Adds repeated value twice(M, X, C, I) [3000, 300, 30, 3]
    // Number at thousands place not greater than 3 so we default the thousands place value to 0 for >3
    "4": [0, "CD", "XL", "IV"], // [0, 400, 40, 4]
    "5": [0, "D", "L", "V"], // [0, 500, 50, 5]
    "6": six,
    "7": six.map(i => { // Adds repeated value (C, X, I) to respective elements
      if (typeof(i) !== "number") i+= i[i.length -1] // Skips over 0 in loop and adds repeated value
      return i
    }), // [0, 700, 70, 7]
    "8": six.map(i => { // Adds repeated value (C, X, I) twice to respective elements
      if (typeof(i) !== "number") i+= i[i.length -1].repeat(2) //Skips over 0 in loop and adds value
      return i
    }), // [0, 800, 80, 8]
    "9": [0, "CM", "XC", "IX"] // [0, 900, 90, 9]

  }

  num += "" 
  while (num.length < 4) // Adds 0 in the beginning of number to make sure it is 4 digits
    num = "0" + num
  
  let romanNum = ''

  for (let i = 0; i < num.length; i++) {
    romanNum += romanNumerals[num[i]][i]
    // num[i] stores the digit at that place
    // i stores the digits place in (Thousands, Hundreds, Tens, Ones)
    // i corresponds to the index number in the list for the key num[i]
  }
  console.log(romanNum)
 return romanNum;
}

convertToRoman(1984);

One way to do it would be to put the main Roman symbols for 1, 5, 10, 50, 100, 500 and 1000 in a lookup object with key- value pairs. The key would be the number which would fetch the corresponding Roman value.
The rest of the numbers can be achieved by a repetition/combination of these core numbers.

So if you check the reference link given in this fCC project, the Math Is Fun Website splits the number into thousands, hundreds, tens and ones place. So you would need to do that in the first step.
Second step would be to have switch constructs - one for each of these places of thousandths, hundredths, tens and ones which contain cases matching the number in each of these places. Depending on the match, the matching case should give its Roman equivalent. And append each of these values returned from each switch constructs for the thousandths, hundredths, tens, ones place, etc. to each other to form the final Roman numeral string.

Begin with an empty string str.
For example, 125 would be split as 100, 20, 5. So the switch construct for hundredths place should return C when 1 is passed to it. Append C to str. Then the switch construct for the tenths place should return XX when 2 is passed to it. Append this XX to str which is currently C. So now str is CXX. And then pass 5 to the ones place switch construct. it should return V. Append this V to str. So now str will be CXXV finally.

So for this, there should be four switch constructs, one for each of the thousandths, hundredths, tens and ones place with each switch statement having cases from 0 through 9, to return each number in its Roman equivalent depending on its place value.

It will be more lines of code but definitely neater and more understandable.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.