• API
• FAQ
• Tools
• Archive
daily pastebin goal
73%
SHARE
TWEET

Untitled

a guest Mar 26th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function convertToRoman(num) {
2.   let numArr = num.toString().split("");
3.   let numSize = numArr.length;
4.
5.   let onesArr = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
6.   let tensArr = ['X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX','XC'];
7.   let hundredsArr = ['C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC','CM'];
8.   let thousandsArr = ['M', 'MM', 'MMM', 'MMMM'];
9.
10.   if(numSize === 1){
11.     return oneDigitToRoman(numArr[0], onesArr);
12.   }
13.   else if(numSize === 2){
14.     return oneDigitToRoman(numArr[0], tensArr)
15.           + oneDigitToRoman(numArr[1], onesArr);
16.   }
17.   else if(numSize === 3){
18.     return oneDigitToRoman(numArr[0], hundredsArr)
19.       + oneDigitToRoman(numArr[1], tensArr)
20.       + oneDigitToRoman(numArr[2], onesArr);
21.   }
22.   else if(numSize === 4){
23.   return oneDigitToRoman(numArr[0], thousandsArr)
24.     + oneDigitToRoman(numArr[1], hundredsArr)
25.     + oneDigitToRoman(numArr[2], tensArr)
26.     + oneDigitToRoman(numArr[3], onesArr);
27.   }
28. }
29.
30. function oneDigitToRoman(num, arr){
31.   if(num == 0){
32.     return "";
33.   }
34.   return arr[num - 1];
35. }
36.
37. convertToRoman(36);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top