# Ruman Number Converter

**URL:** <https://forum.freecodecamp.org/t/ruman-number-converter/141069>\
**Category:** General\
**Created:** [August 11, 2017, 12:19am UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069 "2017-08-11T00:19:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [August 11, 2017, 12:19am UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/1 "2017-08-11T00:19:38Z")

</div>

Hi all  
I have passed the challenge of Ruman Numeral Converter. The issue is when I looked at the other solutions at the hint topic, I feel so Fool because my solution was so basic compared to other programme solutions. What I need is to tell me if I am wrong or I don’t know how to think as a programmer ?

Hint page link to see other solutions:

> [@freeCodeCamp Challenge Guide: Roman Numeral Converter](https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-roman-numeral-converter/16044):
>
> [Roman Numeral Converter](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter) Hints/Solutions are not provided for certification projects as these are intended to be a test of your learning and understanding.

my Solu :

```auto
function convertToRoman(num) {
  /* 
  Main IDea
  convert num to str 
  convert each str char to it's corresponding Roman number considering it's order 
  join the result 
  */ 
  var str ='';
  str += num;
  var result = [];
  var res1 = '';
  var res2 = '';
  var res3 = '';
  var res4 = '';
  if (str.length == 0){
  result = [];
  }else if (str.length > 0){
    switch (str[str.length-1]){
    case "1" :
    res1 = "I";
    break;
    case "2" :
    res1 = "II";
    break;
      case "3" :
    res1 = "III";
    break;
      case "4" :
    res1 = "IV";
    break;
      case "5" :
    res1 = "V";
    break;
      case "6" :
    res1 = "VI";
    break;
      case "7" :
    res1 = "VII";
    break;
      case "8" :
    res1 = "VIII";
    break;
      case "9" :
    res1 = "IX";
    break;
      
  }
    switch (str[str.length-2]){
        
      case "1" :
    res2 = "X";
    break;
      case "2" :
    res2 = "XX";
    break;
      case "3" :
    res2 = "XXX";
    break;
      case "4" :
    res2 = "XL";
    break;
      case "5" :
    res2 = "L";
    break;
      case "6" :
    res2 = "LX";
    break;
      case "7" :
    res2 = "LXX";
    break;
      case "8" :
    res2 = "LXXX";
    break;
      case "9" :
    res2 = "XC";
    break;
    }
    switch (str[str.length-3]){
        
      case "1" :
    res3 = "C";
    break;
      case "2" :
    res3 = "CC";
    break;
      case "3" :
    res3 = "CCC";
    break;
      case "4" :
    res3 = "CD";
    break;
      case "5" :
    res3 = "D";
    break;
      case "6" :
    res3 = "DC";
    break;
      case "7" :
    res3 = "DCC";
    break;
      case "8" :
    res3 = "DCCC";
    break;
      case "9" :
    res3 = "CM";
    break;
    }
    switch (str[str.length-4]){
        
      case "1" :
    res4 = "M";
    break;
      case "2" :
    res4 = "MM";
    break;
      case "3" :
    res4 = "MMM";
    break;
    }
  }
result.push(res4,res3,res2,res1);
  return result.join("");
}

convertToRoman(39999);

```

---

<div class="post-metadata">

**Author:** ![JashuHB](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jashuhb/32/62452_2.png) [@JashuHB](https://forum.freecodecamp.org/u/JashuHB)\
**Post date:** [August 11, 2017, 2:16am UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/2 "2017-08-11T02:16:00Z")

</div>

I would say if it works you don’t have to worry so much. The most important part is that you did come up with a working solution, efficiency in your code comes with time and practice. You just need to apply the conscientiousness you show now to improving your coding in the future.

I used to think the same way when on another website I’d finish a challenge and see the top solutions by other people, sometimes It turned out I was using 10 times more lines of code than the top guys. You will see faster ways to do things as time goes on, and you’ll learn how to better reuse parts of your code as functions.

I think as you take challenges involving checking and manipulating data in multidimensional arrays, you will more fully understand the power of arrays, looping through arrays, and general patterns to this kind of problem solving.

Good Luck!

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [August 11, 2017, 3:09am UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/3 "2017-08-11T03:09:39Z")

</div>

I cleaned up your code.  
You need to use triple backticks to post code to the forum.  
See [this post for details](http://forum.freecodecamp.com/t/forum-code-formatting/25574).

---

<div class="post-metadata">

**Author:** ![Soupedenuit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/soupedenuit/32/32478_2.png) [@Soupedenuit](https://forum.freecodecamp.org/u/Soupedenuit)\
**Post date:** [August 11, 2017, 4:08am UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/4 "2017-08-11T04:08:23Z")

</div>

There’s always someone out there with a better, simpler, fancier code. Don’t worry too much about it. If it works, it works. As you progress in your studies you will improve. You can revisit your solutions later and refine or redo them. It’s not a bad idea to revisit your work as you get better.

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [August 13, 2017, 3:37pm UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/5 "2017-08-13T15:37:43Z")

</div>

That makes me feel good and motivated. I only worried about being a good programmer and felt so fool when I looked at other solutions. My code was so primary with no special idea or touch.But, as Yoo said I’ll keep going and try to improve more. Thank you

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [August 13, 2017, 3:39pm UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/6 "2017-08-13T15:39:34Z")

</div>

I was searching for the Method because an admin edited another post for me before. Thank you

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [August 13, 2017, 3:40pm UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/7 "2017-08-13T15:40:37Z")

</div>

Thank you buddy. This gives me Hope. I’ll keep going

---

<div class="post-metadata">

**Author:** ![TeslaSoftware](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/teslasoftware/32/158678_2.png) [@TeslaSoftware](https://forum.freecodecamp.org/u/TeslaSoftware)\
**Post date:** [February 25, 2018, 6:06pm UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/8 "2018-02-25T18:06:39Z")

</div>

Here is my solution, hope you will find it useful . After setting up the lookup table there are only few lines of code.

```auto
function convertToRoman(num) {
	
	var arabicToRoman = {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", 2000: "MM", 3000: "MMM"};

  var digits = String(+num).split(""); //array with digits
  var result = ''; //empty result initialized
  for(var i =0; i <digits.length; i++){
    var lookupKey = digits[i]*Math.pow(10,digits.length-i-1); //find the key of the current index value multiplied by power of 10^i  
    if(arabicToRoman[lookupKey]){
      result += arabicToRoman[lookupKey];
    }    
  }
  return result;    
    
}

```

---

<div class="post-metadata">

**Author:** ![Ahmed-Elbessfy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ahmed-elbessfy/32/38737_2.png) [@Ahmed-Elbessfy](https://forum.freecodecamp.org/u/Ahmed-Elbessfy)\
**Post date:** [February 26, 2018, 10:31pm UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/9 "2018-02-26T22:31:10Z")

</div>

This is also better than my solution. Good Work

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 6:01pm UTC](https://forum.freecodecamp.org/t/ruman-number-converter/141069/10 "2021-01-16T18:01:55Z")

</div>

Closed on January 16, 2021, 6:01pm UTC


