Post your Roman Numeral Converter solutions here!

Bravo, this is the very simples solution that I saw till now. Thank you for sharing it.

1 Like

Thank you very much Simjan :pray: :pray:

Hi everyone!

Here is mine:

function convertToRoman(num) {
 
    //This converts the number to a string and then to a comma-separated list of characters
    let arr = num.toString().split("");
    //Let's break down the number into units, tens, hundreds, thousands...
    let iNum = arr.pop();
    let xNum = arr.pop();
    let cNum = arr.pop();
    let mNum = arr.pop();

    if (iNum < 4) {
        iNum = "I".repeat(iNum.valueOf()); //Here the possible results will be: Ix1 = I, Ix2 = II, Ix3 = III
    } else if (iNum == 4) {
        iNum = "I" + "V" // I+V = IV
    } else if (iNum > 4 && iNum < 9) { 
        iNum = "V" + "I".repeat(iNum.valueOf() - 5)//V, VI, VII, VIII
    } else if (iNum == 9) {
        iNum = "I" + "X" // IX
    } else {
        iNum = ""
    }

    if (xNum < 4) {
        xNum = "X".repeat(xNum.valueOf());
    } else if (xNum == 4) {
        xNum = "X" + "L"
    } else if (xNum > 4 && xNum < 9) {
        xNum = "L" + "X".repeat(xNum.valueOf() - 5)
    } else if (xNum == 9){
        xNum = "X" + "C"
    } else {
        xNum = ""
    }

    if (cNum < 4) {
        cNum = "C".repeat(cNum.valueOf());
    } else if (cNum == 4) {
        cNum = "C" + "D"
    } else if (cNum > 4 && cNum < 9) {
        cNum = "D" + "C".repeat(cNum.valueOf() - 5)
    } else if (cNum == 9){
        cNum = "C" + "M"
    } else {
        cNum = ""
    }

    if (mNum < 9) {
        mNum = "M".repeat(mNum.valueOf());
    } else {
         mNum = ""
    }

  return mNum + cNum + xNum + iNum // Let's concatenate the characters
  
}

console.log(convertToRoman(99));
1 Like
function convertToRoman( num ) {
    const bases = [
        {symbol: 'M', symbolValue: 1000},
        {symbol: 'CM', symbolValue: 900},
        {symbol: 'D', symbolValue: 500},
        {symbol: 'CD', symbolValue: 400},
        {symbol: 'C', symbolValue: 100},
        {symbol: 'XC', symbolValue: 90},
        {symbol: 'L', symbolValue: 50},
        {symbol: 'XL', symbolValue: 40},
        {symbol: 'X', symbolValue: 10},
        {symbol: 'IX', symbolValue: 9},
        {symbol: 'V', symbolValue: 5},
        {symbol: 'IV', symbolValue: 4},
        {symbol: 'I', symbolValue: 1},
    ]
    return converter (num, bases ).join('')
} 

function converter (num, bases, result = [] ) {

    if  (num == 0)
        return;

    const a = bases.find(base => num >= base.symbolValue)
    result.push(a.symbol);
    converter(num - a.symbolValue, bases, result);
    return result 
} 
console.log(convertToRoman(1987));
1 Like

Your first post? Welcome!

I don’t know if I’m missing something but why the result=[] parameter?

1 Like

Hi, @johannesvansc2679278 . Thank you!!!

I’m very happy to by here :smiley:

I’ve passed the result = [] as a opcional parameter to keep passing the result array on each iteration of my recursive function this way I keep tranking the previous number, I was thinking in terms of dinamyc programming .

My bad, I missed the fact that it is recursive :slight_smile:

I must admit I don’t fully understand it yet. But I wonder if the passing down of the array is necessary. I’m not sure. Each function call will return a result. The last one, the base case, returns an empty array for example. If you join the previous results as you get them of the stack the result at the top level will be the converted string.

I must just be missing something. I’m not trying to be critical I’m just asking out of curiosity. It is your solution, and it works according to your way of thinking. And that is not bad.

It has happened to me many times that I write something in way and then after some time when I look at it again, I can’t understand why I did it that way. We all develop over time.

Enjoy the course and develop your style!

1 Like

@johannesvansc2679278 thanks for the advice.

For sure, I apreciate code reviews , and your way of thinking make sense.

Here another way:

function convertToRoman( num ) {
    const bases = [
        {symbol: 'M', symbolValue: 1000},
        {symbol: 'CM', symbolValue: 900},
        {symbol: 'D', symbolValue: 500},
        {symbol: 'CD', symbolValue: 400},
        {symbol: 'C', symbolValue: 100},
        {symbol: 'XC', symbolValue: 90},
        {symbol: 'L', symbolValue: 50},
        {symbol: 'XL', symbolValue: 40},
        {symbol: 'X', symbolValue: 10},
        {symbol: 'IX', symbolValue: 9},
        {symbol: 'V', symbolValue: 5},
        {symbol: 'IV', symbolValue: 4},
        {symbol: 'I', symbolValue: 1},
    ]
    return converter (num, bases )
} 

function converter (num, bases ) {
    if  (num == 0) return [];
    const number = bases.find(base => num >= base.symbolValue)
    return number.symbol + converter(num - number.symbolValue, bases);
} 

console.log(convertToRoman(1963));

Thnaks for the advice

3 Likes

This is my looong solution; i didn;t consult google for any ideas, just my raw thinking based on my knowledge of JS so far:

function convertToRoman(num) {
 let str = num.toString();
 let strArray = str.split(''); 
 let strLen = str.length;
 
//convert all array elements to integers
for (let i=0; i < strArray.length ; i++) {
	strArray[i] = parseInt(strArray[i]);  
}
  //convert numbers to Roman Numerals
  if (strLen == 1){

     switch (strArray[0] = strArray[0]) {
      case 1:
        strArray = 'I';
        break;
      case 2:
        strArray = 'II';
        break;
      case 3:
        strArray = 'III';
        break;
      case 4:
        strArray = 'IV';
        break;
      case 5:
        strArray = 'V';
        break;
      case 6:
        strArray = 'VI';
        break;
      case 7:
        strArray = 'VII';
        break;
      case 8:
        strArray = 'VIII';
        break;
      case 9:
        strArray = 'IX';
        break;
    }
  return strArray;
  
  
  }
  if (strLen == 2){
    strArray[0] = strArray[0] *10;//add appropriate zeros
    strArray[1] = strArray[1] * 1;
    
   switch (strArray[0] = strArray[0]) {
     case 10:
        strArray[0] = 'X';
        break;
      case 20:
        strArray[0] = 'XX';
        break;
      case 30:
        strArray[0] = 'XXX';
        break;
      case 40:
        strArray[0] = 'XL';
        break;
      case 50:
        strArray[0] = 'L';
        break;
      case 60:
        strArray[0] = 'LX';
        break;
      case 70:
        strArray[0] = 'LXX';
        break;
      case 80:
        strArray[0] = 'LXXX';
        break;
      case 90:
        strArray[0] = 'XC';
        break;
    }
    switch (strArray[1] = strArray[1]){
      case 0:
        strArray[1] = '';
        break;
      case 1:
        strArray[1] = 'I';
        break;
      case 2:
        strArray[1] = 'II';
        break;
      case 3:
        strArray[1] = 'III';
        break;
      case 4:
        strArray[1] = 'IV';
        break;
      case 5:
        strArray[1] = 'V';
        break;
      case 6:
        strArray[1] = 'VI';
        break;
      case 7:
        strArray[1] = 'VII';
        break;
      case 8:
        strArray[1] = 'VIII';
        break;
      case 9:
        strArray[1] = 'IX';
        break;
    }
    return strArray.join('');//return elements stringed together without commas and spaces
    
  }
  if (strLen == 3){
    strArray[0] = strArray[0] * 100;//add appropriate zeros
    strArray[1] = strArray[1] * 10;
    strArray[2] = strArray[2] * 1;

    switch (strArray[0] = strArray[0]){
      case 100:
        strArray[0] = 'C';
        break;
      case 200:
        strArray[0] = 'CC';
        break;
      case 300:
        strArray[0] = 'CCC';
        break;
      case 400:
        strArray[0] = 'CD';
        break;
      case 500:
        strArray[0] = 'D';
        break;
      case 600:
        strArray[0] = 'DC';
        break;
      case 700:
        strArray[0] = 'DCC';
        break;
      case 800:
        strArray[0] = 'DCCC';
        break;
      case 900:
        strArray[0] = 'CM';
        break;
    }
switch (strArray[1] = strArray[1]){
      case 0:
        strArray[1] = '';
        break;
      case 10:
        strArray[1] = 'X';
        break;
      case 20:
        strArray[1] = 'XX';
        break;
      case 30:
        strArray[1] = 'XXX';
        break;
      case 40:
        strArray[1] = 'XL';
        break;
      case 50:
        strArray[1] = 'L';
        break;
      case 60:
        strArray[1] = 'LX';
        break;
      case 70:
        strArray[1] = 'LXX';
        break;
      case 80:
        strArray[1] = 'LXXX';
        break;
      case 90:
        strArray[1] = 'XC';
        break;
    }
    switch (strArray[2] = strArray[2]){
      case 0:
        strArray[2] = '';
        break;
      case 1:
        strArray[2] = 'I';
        break;
      case 2:
        strArray[2] = 'II';
        break;
      case 3:
        strArray[2] = 'III';
        break;
      case 4:
        strArray[2] = 'IV';
        break;
      case 5:
        strArray[2] = 'V';
        break;
      case 6:
        strArray[2] = 'VI';
        break;
      case 7:
        strArray[2] = 'VII';
        break;
      case 8:
        strArray[2] = 'VIII';
        break;
      case 9:
        strArray[2] = 'IX';
        break;
    }
  return strArray.join('');
    
  }
  if (strLen == 4){
    strArray[0] = strArray[0] * 1000;
    strArray[1] = strArray[1] * 100;
    strArray[2] = strArray[2] * 10;
    strArray[3] = strArray[3] * 1;

    switch (strArray[0] = strArray[0]){
      case 1000:
        strArray[0] = 'M';
        break;
      case 2000:
        strArray[0] = 'MM';
        break;
      case 3000:
        strArray[0] = 'MMM';
        break;
      case 4000:
        strArray[0] = 'MMMM'
    }
    switch (strArray[1] = strArray[1]){
      case 0:
        strArray[1] = '';
        break;
      case 100:
        strArray[1] = 'C';
        break;
      case 200:
        strArray[1] = 'CC';
        break;
      case 300:
        strArray[1] = 'CCC';
        break;
      case 400:
        strArray[1] = 'CV';
        break;
      case 500:
        strArray[1] = 'V';
        break;
      case 600:
        strArray[1] = 'VC';
        break;
      case 700:
        strArray[1] = 'VCC';
        break;
      case 800:
        strArray[1] = 'VCCC';
        break;
      case 900:
        strArray[1] = 'CM';
        break;
    }
switch (strArray[2] = strArray[2]){
      case 0:
        strArray[2] = '';
        break;
      case 10:
        strArray[2] = 'X';
        break;
      case 20:
        strArray[2] = 'XX';
        break;
      case 30:
        strArray[2] = 'XXX';
        break;
      case 40:
        strArray[2] = 'XL';
        break;
      case 50:
        strArray[2] = 'L';
        break;
      case 60:
        strArray[2] = 'LX';
        break;
      case 70:
        strArray[2] = 'LXX';
        break;
      case 80:
        strArray[2] = 'LXXX';
        break;
      case 90:
        strArray[2] = 'XC';
        break;
    }
    switch (strArray[3] = strArray[3]){
      case 0:
        strArray[3] = '';
        break;
      case 1:
        strArray[3] = 'I';
        break;
      case 2:
        strArray[3] = 'II';
        break;
      case 3:
        strArray[3] = 'III';
        break;
      case 4:
        strArray[3] = 'IV';
        break;
      case 5:
        strArray[3] = 'V';
        break;
      case 6:
        strArray[3] = 'VI';
        break;
      case 7:
        strArray[3] = 'VII';
        break;
      case 8:
        strArray[3] = 'VIII';
        break;
      case 9:
        strArray[3] = 'IX';
        break;
    }
    return strArray.join('');
  }

}

convertToRoman(2);
convertToRoman(36);
convertToRoman(891);
convertToRoman(3999);
convertToRoman(1006);

Your solution is awesome. It is quite easy to understand. code readability = 3000;

so0oO r3@dabl3, siMpl3, & 3ffici3nt! :joy:

// 🎉 Completed on 3-26-22 D25_W4_R1 of #100DaysOfCode 
function convertToRoman(num) {
  // number to string to array
  let str = num.toString();
  let arr = str.split("")
  // storage for expanded form and roman numeral
  let newArr = []
  let roman = "";
  // roman numeral table
  const lookup = {
    4:"IV", 9:"IX", 40:"XL", 90:"XC", 400:"CD", 900:"CM",
    1: "I", 2: "II", 3: "III", 5: "V", 6: "VI", 7: "VII",
    8: "VIII", 10: "X", 20: "XX", 30: "XXX", 50: "L", 60: "LX",
    70: "LXX", 80: "LXXX", 100: "C", 200: "CC", 300: "CCC", 500: "D",
    600: "DC", 700: "DCC", 800: "DCCC", 1000: "M", 2000: "MM", 3000: "MMM"
  };
  // ones
  if(arr.length === 1){
    newArr.push(arr[0]*1);
  }
  // tens
  if(arr.length === 2){
    newArr.push(arr[0]*10);
    newArr.push(arr[1]*1);
  }
  // hundreds
  if(arr.length === 3){
    newArr.push(arr[0]*100);
    newArr.push(arr[1]*10);
    newArr.push(arr[2]*1);
  }
  // one thousands
  if(arr.length === 4){
    newArr.push(arr[0]*1000);
    newArr.push(arr[1]*100);
    newArr.push(arr[2]*10);
    newArr.push(arr[3]*1);
  }
  // lookup at table
  for(let i = 0; i < newArr.length; i++){
    if(lookup.hasOwnProperty(newArr[i]) === true){
      roman += lookup[newArr[i]];
    }
  }
  return roman;
};
console.log(convertToRoman(3999));