[FEEDBACK] Roman Numeral Converter

Hi, i don’t know if this is the right place to ask but since i’m pretty happy with my result, i wanted to ask you fellow campers to review my solution of the Roman Numeral Converter challenge!

Here it is:

function convertToRoman(num) {
  var decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  var roman = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
  var result = '';
  
  for(var i = 0; i < decimal.length; i++) {
    while(num >= decimal[i]) {
      result += roman[i];
      num -= decimal[i];
    }
  }
  
 return result;
}

convertToRoman(36);
1 Like

Hi! This is pretty similar to the way I solved this challenge, but I think your version is a little more satisfying. It’s simple and easy to read.

Here’s mine, if you’re curious:

var roman = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'];
var arabic = [1000,900,500,400,100,90,50,40,10,9,5,4,1];

function convertToRoman(num) {
  var result = [];
    for (i = 0; i < roman.length; i++) {
      while ((num - arabic[i]) >= 0) {
          result.push(roman[i]);
          num -= arabic[i];
      } 
  }
  return result.join("");
}
2 Likes

Wow, that’s beautifully simple!
Mine is overly complicated (solved about a month ago):

[details=My solution:]

 function convertToRomanNum(num) {
   
 /* Code is valid up to 399,999 because I did not include the symbol 
  *  for 500,000 required for 400,000+. Note that Ṽ, Ẋ, Ḹ, Č, were used 
  *   because I couldn't figure out how to apply overlines to letters. 
  *   "I" is equal to 1
  *   "V" is equal to 5
  *   "X" is equal to 10
  *   "L" is equal to 50
  *   "C" is equal to 100
  *   "D" is equal to 500
  *   "M" is equal to 1,000
  *   "Ṽ" is equal to 5,000
  *   "Ẋ" is equal to 10,000
  *   "Ḹ" is equal to 50,000
  *   "Č" is equal to 100,000
  */
  
  const characters = {
    1: "I",
    2: "V",
    3: "X",
    4: "L",
    5: "C",
    6: "D",
    7: "M",
    8: "Ṽ",
    9: "Ẋ",
    10: "Ḹ",
    11: "Č"
  };
  
  var roman = [];
  var numArr = num.toString().split('');
  
  const digits = {
    1: numArr[numArr.length - 1],
    3: numArr[numArr.length - 2],
    5: numArr[numArr.length - 3],
    7: numArr[numArr.length - 4],
    9: numArr[numArr.length - 5],
    11: numArr[numArr.length - 6]
  };
  
  for (let i = 1; i < numArr.length * 2; i = i + 2) {
    
    if (digits[i] > 0 && digits[i] <= 3) {
      for (let j = 0; j < digits[i]; j++) {
      roman.unshift(characters[i]);
      }
    }
  
    else if (digits[i] == 4) {
      roman.unshift(characters[i + 1]);
      roman.unshift(characters[i]);
    }
  
    else if (digits[i] == 5) {
      roman.unshift(characters[i + 1]);
    }
  
    else if (digits[i] >= 6 && digits[i] <= 8) {
      for (let j = 5; j < digits[i]; j++) {
        roman.unshift(characters[i]);
      }
        roman.unshift(characters[i + 1]);
    }
  
    else if (digits[i] == 9) {
      roman.unshift(characters[i + 2]);
      roman.unshift(characters[i]);
    }
  
    else ;
    
  } //closing bracket for outer condition
  
  var result = roman.join('');
  
  return num + " converted to a Roman numeral is " + result;
}

convertToRomanNum(1973);
```[/details]
1 Like

wow, mine is also too complicated compare to yours.

Thanks for sharing!

var romanNumbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var romanSymbols = {
  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 findSymbol(num) {
  
  for (var i = 0; i < romanNumbers.length; i++ ){
   if (num >= romanNumbers[i]) {
     return romanNumbers[i];
   }
  }
  
}

function convert(num) {
  
  var remainNum = num;
  var romanNum = "";
  var tempNum = 0;
  
  while (remainNum > 0) {
    tempNum = findSymbol(remainNum);
    romanNum += romanSymbols[tempNum];
    remainNum -= tempNum;
  }
  
  return romanNum;
  
}

convert(3999);

A couple of things to note; (standard) roman numerals cannot go higher than 4,999 and you have to use “MMMM” to represent 4,000.

I didn’t check your solutions closely but it doesn’t appear they account for these, except for Soupedenuit who has “roman numerals” for numbers greater than 1000. Where did you come up with those?

My solution is here:

  • Bruce

Thank you all for answering to my thread!

@mrthnmn If i remember correctly, the 4000 in roman numerals is represented by an IV with a “bar” on top of it, so i could add that, but i should use Unicode in that case, probably.

This is quite nice! Much better than the way I did it, which was to basically split the original number into an array, count the number of places (units, decimals, etc.), and then use a switch statement to substitute in the appropriate roman numeral.

I had never heard that. That must be where Soupedenuit got the roman numerals beyond M.

I really love the forum, my code is very complicated, but it works and i’m learning … my problem solving efficiency will increase soon


function convertToRoman(num) {

  var objConversion = {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"};
  
  var resultArr = [];
  var counter = num;
    
do{
  
  if(num >= 1000 ){
    thousandsFunction();
  }
  
  if (num > 99 && num <= 999){
    hundredsFunction();
  }else if (num > 9 && num <= 99){
    tensFunction();
  }else if (num >0 && num <= 9){
    onesFunction();
  }
  
   
} while(num > 0);

function onesFunction (){
    var oH = 0;
  
    oH = num ;
    num = num - oH ;
    resultArr.push(objConversion[oH]);
    return;
    
  }
  
  function tensFunction (){
    var tH = 0;
    var sH = "";
    
    tH = num / 10;
    sH = tH = Math.floor(tH) * 10;
    num = num - tH;
        
    resultArr.push(objConversion[tH]);
    return;
  }
  
  function hundredsFunction (){
    var hH = 0;
    
    hH = num / 100;
    hH = Math.floor(hH) * 100;
    num = num - hH;
    resultArr.push(objConversion[hH]);
    
    return resultArr;
  }
  
    function thousandsFunction (){
    var thH = 0;
    var x;
      
    x = thH = num / 1000;
    thH = Math.floor(thH) * 1000;
    num = num - thH;
    
    for(var i = 0; i < Math.floor(x); i++ ){
    resultArr.push(objConversion[1000]);
    }
      
    return;
  }
  
  console.log(resultArr.join(""));
  
  return resultArr.join("");
}
convertToRoman(36);

beautiful code this!!! wow.

1 Like

Thank you for the compliment!