Guest User

Untitled

a guest
Apr 1st, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function convertToRoman(num) {
  2.     // key
  3.     var numForKey = [1, 5, 10, 50, 100, 500, 1000];
  4.     var lettersForKey = ['I', 'V', 'X', 'L', 'C', 'D', 'M'];
  5.    
  6.     // Extended key
  7.     var numForExtendedKey = [4, 9, 40, 90, 400, 900];
  8.     var lettersForExtendedKey = ['IV', 'IX', 'XL', 'XC', 'CD', 'CM'];
  9.    
  10.     // Splitting the number provided by user.
  11.     var splitNum = (num).toString(10).split("").map(Number);
  12.    
  13.     // Will hold the split numbers after they have been multiplied by the correct power.
  14.     var multipliedNums = [];
  15.    
  16.     // Multiplying split numbers by the correct power (eg. 99 will become [90, 9]). This will
  17.     // allow for conversion to numerals.
  18.     if (splitNum.length == 4) {
  19.         multipliedNums.push(splitNum[0] * 1000);
  20.         multipliedNums.push(splitNum[1] * 100);
  21.         multipliedNums.push(splitNum[2] * 10);
  22.         multipliedNums.push(splitNum[3]);
  23.     } else if (splitNum.length == 3) {
  24.         multipliedNums.push(splitNum[0] * 100);
  25.         multipliedNums.push(splitNum[1] * 10);
  26.         multipliedNums.push(splitNum[2]);
  27.     } else if (splitNum.length == 2) {
  28.         multipliedNums.push(splitNum[0] * 10);
  29.         multipliedNums.push(splitNum[1]);
  30.     } else {
  31.         multipliedNums.push(splitNum[0]);
  32.     }
  33.    
  34.     // This array will hold the numerals as they are converted by the below for loop.
  35.     var numerals = [];
  36.    
  37.     for (var i = 0; i < multipliedNums.length; i++) {
  38.         // If the user's number is identical to a numeral's number, it gets pushed.
  39.         // Else, it checks if the number is equal to 2 or 3 times the number. If so,
  40.         // the numeral is pushed the correct number of times.
  41.         if (numForKey.indexOf(multipliedNums[i]) != -1) {
  42.             numerals.push(lettersForKey[numForKey.indexOf(multipliedNums[i])]);
  43.         } else if (numForExtendedKey.indexOf(multipliedNums[i]) != -1) {
  44.             numerals.push(lettersForExtendedKey[numForExtendedKey.indexOf(multipliedNums[i])]);
  45.         } else {
  46.             for (var z = 0; z < numForKey.length; z++) {
  47.                 if (multipliedNums[i] == numForKey[z] * 2) {
  48.                     numerals.push(lettersForKey[z]);
  49.                     numerals.push(lettersForKey[z]);
  50.                     break;
  51.                 } else if (multipliedNums[i] == numForKey[z] * 3) {
  52.                     numerals.push(lettersForKey[z]);
  53.                     numerals.push(lettersForKey[z]);
  54.                     numerals.push(lettersForKey[z]);
  55.                     break;
  56.                 } else if (z == numForKey.length - 1) {
  57.                     var toPush = complexNumeral(multipliedNums[i]);
  58.                     toPush = toPush.join("");
  59.                     numerals.push(toPush);
  60.                     break;
  61.                 }
  62.             }
  63.         }
  64.     }
  65.    
  66.     // This function will convert complex numerals (that require more than 1
  67.     // numeral to represent the number correctly).
  68.     // Converted numerals are returned in an array.
  69.     function complexNumeral(item) {
  70.         var toPush = [];
  71.         var remainder;
  72.         for (var n = numForKey.length; n > 0; n--) {
  73.             if (numForKey[n] < item) {
  74.                 toPush.push(lettersForKey[n]);
  75.                 remainder = item % numForKey[n];
  76.                 n = 0;
  77.             }
  78.         }
  79.        
  80.         if (numForKey.indexOf(remainder) != -1) {
  81.             toPush.push(lettersForKey[numForKey.indexOf(remainder)]);
  82.             return toPush;
  83.         }
  84.        
  85.         for (var l = 0; l < numForKey.length; l++) {
  86.             if (remainder == numForKey[l] * 2) {
  87.                 toPush.push(lettersForKey[l]);
  88.                 toPush.push(lettersForKey[l]);
  89.             } else if (remainder == numForKey[l] * 3) {
  90.                 toPush.push(lettersForKey[l]);
  91.                 toPush.push(lettersForKey[l]);
  92.                 toPush.push(lettersForKey[l]);
  93.             }
  94.         }
  95.         return toPush;
  96.     }
  97.    
  98.     numerals = numerals.join("");
  99.     return numerals;
  100. }
Add Comment
Please, Sign In to add comment