Ruman Number Converter

Ruman Number Converter
0

#1

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:

my Solu :

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);

#2

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!


#3

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#4

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.


#5

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


#6

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


#7

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


#8

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

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;    
    
}


#9

This is also better than my solution. Good Work