Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function romanNumeral(number) {
  2.     var input = number;
  3.     var romSinglesChar, romFourChar, romFivesChar, romTensChar, romHundredsChar = '';
  4.     var romanNumeral = [];
  5.     //minimal input validation
  6.     if (input > 1000) {
  7.         console.log("input over limit");
  8.         return;
  9.     }
  10.     //reverse the String
  11.     var reversedInput = input.split("").reverse().join("");
  12.     //console.log(reversedInput);
  13.     //process one digit at a time
  14.     for (index = 1; index <= reversedInput.length; index++) {
  15.         if (reversedInput.length <= 4) {
  16.             var digit = reversedInput[index - 1];
  17.             //calculate the max: 1000
  18.             if (digit == "1" && index == 4) {
  19.                 romanNumeral.push("M");
  20.             }
  21.             //calculate 999 and below
  22.             switch (digit) {
  23.                 //0
  24.                 case "0":
  25.                     break;
  26.                     //1-3
  27.                 case "1":
  28.                 case "2":
  29.                 case "3":
  30.                     romSinglesChar = getRomanSinglesChar(index);
  31.                     for (i = 1; i <= parseInt(digit); i++) {
  32.                         romanNumeral.push(romSinglesChar);
  33.                     }
  34.                     break;
  35.                     //4
  36.                 case "4":
  37.                     romFourChar = getRomanFourChar(index);
  38.                     romanNumeral.push(romFourChar);
  39.                     break;
  40.                     //5
  41.                 case "5":
  42.                     romFivesChar = getRomanFivesChar(index);
  43.                     romanNumeral.push(romFivesChar);
  44.                     break;
  45.                     //6-8
  46.                 case "6":
  47.                 case "7":
  48.                 case "8":
  49.                     for (i = 1; i <= parseInt(digit) - 5; i++) {
  50.                         romanNumeral.push(getRomanSinglesChar(index));
  51.                     }
  52.                     romanNumeral.push('V');
  53.                     break;
  54.                     //9
  55.                 case "9":
  56.                     romanNumeral.push(getRomanNinesChar(index))
  57.                     break;
  58.             }
  59.         }
  60.     }
  61.     return romanNumeral.reverse().toString().split(",").join("");
  62. }
  63. //output 1s
  64. function getRomanSinglesChar(index) {
  65.     switch (index) {
  66.         case 1:
  67.             return "I";
  68.         case 2:
  69.             return "X";
  70.         case 3:
  71.             return "C";
  72.     }
  73. }
  74. //output 4s
  75. function getRomanFourChar(index) {
  76.     switch (index) {
  77.         case 1:
  78.             return "IV";
  79.         case 2:
  80.             return "XL";
  81.         case 3:
  82.             return "CD";
  83.     }
  84. }
  85. //output 5s
  86. function getRomanFivesChar(index) {
  87.     switch (index) {
  88.         case 1:
  89.             return 'V';
  90.         case 2:
  91.             return 'L';
  92.         case 3:
  93.             return 'D';
  94.     }
  95. }
  96. //output 9s
  97. function getRomanNinesChar(index) {
  98.     switch (index) {
  99.         case 1:
  100.             return "IX";
  101.         case 2:
  102.             return "XC";
  103.         case 3:
  104.             return "CM";
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement