Roman Numeral Challenge finished!

Hi guys, I just did the Roman Numeral Converter Challenge and wanted to share my solution with you! I know is a bit messy but I am so glad that I finally solved it! Also I would like if you share others solutions so I can improve this one!

var romans =   ["I","II","III","IV","V","VI","VII","VIII","IX","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","M","MM","MMM"];

var decimals = [1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000,2000,3000];

var numberLength = num.toString().length;
var numArr = num.toString(10).split("");
var data         = [];

if(decimals.indexOf(num)!==-1){
return romans[decimals.indexOf(num)];
}
  
for(var i = 0; i <numberLength; i++){

if(numberLength==4 && i === 0 && numArr[i]!==0){

   numArr[i]= numArr[i]+"000";
   data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);

}else if((numberLength==4 ) && i === 1 && numArr[i]!==0){
  
   numArr[i]= numArr[i]+"00";
  data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
  
}else if((numberLength==4 || numberLength===2) && i === 2 && numArr[i]!==0){
  
  numArr[i]= numArr[i]+"0";
  data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
  
}else if((numberLength===2) && i === 0 && numArr[i]!==0){
   numArr[i]= numArr[i]+"0";
  data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
  
}else if((numberLength===3) && i === 0 && numArr[i]!==0){
   numArr[i]= numArr[i]+"00";
  data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
  
}else if((numberLength===3) && i === 1 && numArr[i]!==0){
   numArr[i]= numArr[i]+"0";
  data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
  
} 
else{
   data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
}

 }

 return data.join("");
1 Like

I’ve avoided this problem for a very long time - i first ran into it a few years ago via exercism on their ruby track but they didn’t requre you to get the 4/5/9 stuff right. I’ve always thought it trough using division and mod’s - though doing it in javascript required a slight addition to the division (in ruby when you divide by something you ONLY get the integer response - no decimals unless you specifically set it up as a float but I digress), and people had suggested a variety of ways to do it - but in my brain I always broke it down into ‘thousands, hundreds, tens, and ones’ and converted it from there. I expect that this could be cleaned up even more by putting all the conversion stuff in the object so I can just loop through the object keys - but this is my answer - and I just wondered if I made it more complicated than it needs to be?


function convertToRoman(num) {
  var counter = {};
  var answer = [];
  counter.thousands = Math.floor(num/1000);  
  num %= 1000;
  counter.hundreds = Math.floor(num/100);
  num %= 100;
  counter.tens = Math.floor(num/10);
  num %= 10;
  counter.ones = num;
  function convertPlace(placeholder, single, fiver, niner) {
    var increment = counter[placeholder];
    switch (increment) {
      case 1:
      case 2:
      case 3:
        var x = "";
        for(i=0;i<increment; i++) {
          x += single;
        }
        return x;
      case 4:
        return single+fiver;
      case 5:
        return fiver;
      case 6:
      case 7:
      case 8:
        var y = fiver;
        for(i=5; i<increment; i++) {
          y += single;
        }
        return y;
      case 9:
        return niner;
    }
  }
  answer.push(convertPlace("thousands", "M", "ZZ", "XX"));
  answer.push(convertPlace("hundreds", "C", "D", "CM"));
  answer.push(convertPlace("tens", "X", "L", "XC"));
  answer.push(convertPlace("ones", "I", "V", "IX"));
  var george = "thousands";
  return answer.join("");
}
convertToRoman(9575);