Roman Numeral Converter Feedback

Hi,

Any feedback would be appericated.

Note: At the time of writing this post I felt like I had cheated while working on the roman numeral converter project Found here. I had attempted solving the problem on my own by researching and planning out the problem before solving it with code. My original code didn’t work and while I was writing this post I began cleaning up my original solution (e.g., removing whitespaces, tidying up code, and removing redundant code). I ran the tests again out of curiosity due to the fact the numeral conversion for 3 test had displayed the correct output in the console and surprisingly not just this test case had passed but all of the cases passed!

My original code:
let romanNumerals = {
    1000: 'M',
    900: 'CM',
    500: 'D',
    400: 'CD',
    100: 'C',
    90: 'XC',
    50: 'L',
    40: 'XL',
    10: 'X',
    9: 'IX',
    5: 'V',
    4: 'IV',
    1: 'I'
  };

function convertToRoman(num) {

     // Coverts the object into a 2D array and sorts it from highest to lowest.
    let newRomanNumerals = Object.entries(romanNumerals).sort((a, b) => {
      return b[0] - a[0]
    });
  
    let romanNumeral = "";
      // Iterates through each sub array in the parent array.
    for (let i = 0; i < newRomanNumerals.length; i++) {
      // Iterates through the elements in each sub array.
      while (num >= newRomanNumerals[i][0]) { 
        romanNumeral += newRomanNumerals[i][1];
        num -= newRomanNumerals[i][0];
        }
     }
  
     return romanNumeral;
  }
  
  let result = convertToRoman(3);
  console.log(result);
  

Below is the original post I was writing before I miraculously solved the problem with my original code.

I have been working on the roman numeral converter project. I have somewhat solved the problem once already but I wasn’t satisfied with my solution as I ended up googling portions of the problem and visiting stack overflow leading me to refactor some of my code to the solution shown below.

My code after googling:
 let romanNumerals = [
    [1000, 'M'],
    [900, 'CM'],
    [500, 'D'],
    [400, 'CD'],
    [100, 'C'],
    [90, 'XC'],
    [50, 'L'],
    [40, 'XL'],
    [10, 'X'],
    [9, 'IX'],
    [5, 'V'],
    [4, 'IV'],
    [1, 'I']
  ];

function convertToRoman(num) {
  if (num === 0) {
    return '';
  }
    // Iterates through each sub array in the parent array.
  for (let i = 0; i < romanNumerals.length; i++) {
    // Iterates through the elements in each sub array.
    if (num >= romanNumerals[i][0]) {
      return romanNumerals[i][1] + convertToRoman(num - romanNumerals[i][0]);
    }
  }
}

let result = convertToRoman(44);
console.log(result);

I have now tried solving the problem another way as shown below as I felt like I had cheated, but after looking at the test cases it seems that any of the numerals that appear more than once in an expected answer doesn’t pass the case as my code doesn’t have the logic to handle the test case and only cases with unique numerals pass the tests.

Higher level method (Not fully working):
 let romanNumerals = {
    1000: 'M',
    900: 'CM',
    500: 'D',
    400: 'CD',
    100: 'C',
    90: 'XC',
    50: 'L',
    40: 'XL',
    10: 'X',
    9: 'IX',
    5: 'V',
    4: 'IV',
    1: 'I'
  };

function convertToRoman(num) {
  // Coverts the object into a 2D array and sorts it from highest to lowest.
   let newRomanNumerals = Object.entries(romanNumerals).sort((a, b) => {
    return b[0] - a[0]
  });
    
    return newRomanNumerals.map((val) => {
      let numeral = '';
      if (num >= val[0]) {
        // Add the roman numeral associated with its zero index value.
        numeral += val[1];
        // Subtract the value associated with the roman numeral.
        num -= val[0];
        return numeral;
      }
    }).join(""); 
}

let result = convertToRoman(29);
console.log(result)

With the higher-level method code, I am not entirely sure why it’s not working for roman numerals that appear more than once. I could be missing some logic to handle duplicate numerals or I need to keep hacking away at it.

I wanted to share my experience working on this project with others out there as it has given me confidence in my programming ability and I hope I keep improving.

Thank you for taking the time to read my 2 days of torture :sweat_smile:

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