Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
140
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, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
  4.     var lettersForKey = ['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'];
  5.    
  6.     var splitNum = (num).toString(10).split("").map(Number);
  7.     console.log(splitNum);
  8.    
  9.     var multipliedNums = [];
  10.    
  11.     if (splitNum.length == 4) {
  12.         multipliedNums.push(splitNum[0] * 1000);
  13.         multipliedNums.push(splitNum[1] * 100);
  14.         multipliedNums.push(splitNum[2] * 10);
  15.         multipliedNums.push(splitNum[3]);
  16.     } else if (splitNum.length == 3) {
  17.         multipliedNums.push(splitNum[0] * 100);
  18.         multipliedNums.push(splitNum[1] * 10);
  19.         multipliedNums.push(splitNum[2]);
  20.     } else if (splitNum.length == 2) {
  21.         multipliedNums.push(splitNum[0] * 10);
  22.         multipliedNums.push(splitNum[1]);
  23.     } else {
  24.         multipliedNums.push(splitNum[0]);
  25.     }
  26.    
  27.     console.log(multipliedNums);
  28.    
  29.     var convertedNums = [];
  30.    
  31.     for (var i = 0; i < multipliedNums.length; i++) {
  32.         var remainHolder = 0;
  33.        
  34.         for (var n = 0; n < numForKey.length; n++) {
  35.             if (multipliedNums[i] == numForKey[n]) {
  36.                 convertedNums.push(lettersForKey[n]);
  37.             } else if (multipliedNums[i] == numForKey[n] * 2) {
  38.                 convertedNums.push(lettersForKey[n]);        
  39.                 convertedNums.push(lettersForKey[n]);
  40.             } else if (multipliedNums[i] == numForKey[n] * 3) {
  41.                 convertedNums.push(lettersForKey[n]);
  42.                 convertedNums.push(lettersForKey[n]);
  43.                 convertedNums.push(lettersForKey[n]);
  44.             }
  45.         }
  46.        
  47.         for (var z = numForKey.length; z != 0; z--) {
  48.             if (multipliedNums[i] < numForKey[z]) {
  49.                 convertedNums.push(lettersForKey[z]);
  50.                 remainHolder = numForKey[z] % multipliedNums[i];
  51.  
  52.                 for (var x = 0; x < numForKey.length; x++) {
  53.                     if (remainHolder == numForKey[x]) {
  54.                         convertedNums.push(lettersForKey[x]);
  55.                     } else if (remainHolder == numForKey[x] * 2) {
  56.                         convertedNums.push(lettersForKey[x]);        
  57.                         convertedNums.push(lettersForKey[x]);
  58.                     } else if (remainHolder == numForKey[x] * 3) {
  59.                         convertedNums.push(lettersForKey[x]);
  60.                         convertedNums.push(lettersForKey[x]);
  61.                         convertedNums.push(lettersForKey[x]);
  62.                     }
  63.                    
  64.                     break;
  65.                 }
  66.             }
  67.         }  
  68.     }
  69.    
  70.     console.log(convertedNums);
  71.    
  72.     num = convertedNums.join("");
  73.     console.log(num);
  74.     return num;
  75. }
  76.  
  77.  
  78. convertToRoman(99);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement