Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function convertToRoman(num) {
- // key
- var numForKey = [1, 5, 10, 50, 100, 500, 1000];
- var lettersForKey = ['I', 'V', 'X', 'L', 'C', 'D', 'M'];
- // Extended key
- var numForExtendedKey = [4, 9, 40, 90, 400, 900];
- var lettersForExtendedKey = ['IV', 'IX', 'XL', 'XC', 'CD', 'CM'];
- // Splitting the number provided by user.
- var splitNum = (num).toString(10).split("").map(Number);
- // Will hold the split numbers after they have been multiplied by the correct power.
- var multipliedNums = [];
- // Multiplying split numbers by the correct power (eg. 99 will become [90, 9]). This will
- // allow for conversion to numerals.
- if (splitNum.length == 4) {
- multipliedNums.push(splitNum[0] * 1000);
- multipliedNums.push(splitNum[1] * 100);
- multipliedNums.push(splitNum[2] * 10);
- multipliedNums.push(splitNum[3]);
- } else if (splitNum.length == 3) {
- multipliedNums.push(splitNum[0] * 100);
- multipliedNums.push(splitNum[1] * 10);
- multipliedNums.push(splitNum[2]);
- } else if (splitNum.length == 2) {
- multipliedNums.push(splitNum[0] * 10);
- multipliedNums.push(splitNum[1]);
- } else {
- multipliedNums.push(splitNum[0]);
- }
- // This array will hold the numerals as they are converted by the below for loop.
- var numerals = [];
- for (var i = 0; i < multipliedNums.length; i++) {
- // If the user's number is identical to a numeral's number, it gets pushed.
- // Else, it checks if the number is equal to 2 or 3 times the number. If so,
- // the numeral is pushed the correct number of times.
- if (numForKey.indexOf(multipliedNums[i]) != -1) {
- numerals.push(lettersForKey[numForKey.indexOf(multipliedNums[i])]);
- } else if (numForExtendedKey.indexOf(multipliedNums[i]) != -1) {
- numerals.push(lettersForExtendedKey[numForExtendedKey.indexOf(multipliedNums[i])]);
- } else {
- for (var z = 0; z < numForKey.length; z++) {
- if (multipliedNums[i] == numForKey[z] * 2) {
- numerals.push(lettersForKey[z]);
- numerals.push(lettersForKey[z]);
- break;
- } else if (multipliedNums[i] == numForKey[z] * 3) {
- numerals.push(lettersForKey[z]);
- numerals.push(lettersForKey[z]);
- numerals.push(lettersForKey[z]);
- break;
- } else if (z == numForKey.length - 1) {
- var toPush = complexNumeral(multipliedNums[i]);
- toPush = toPush.join("");
- numerals.push(toPush);
- break;
- }
- }
- }
- }
- // This function will convert complex numerals (that require more than 1
- // numeral to represent the number correctly).
- // Converted numerals are returned in an array.
- function complexNumeral(item) {
- var toPush = [];
- var remainder;
- for (var n = numForKey.length; n > 0; n--) {
- if (numForKey[n] < item) {
- toPush.push(lettersForKey[n]);
- remainder = item % numForKey[n];
- n = 0;
- }
- }
- if (numForKey.indexOf(remainder) != -1) {
- toPush.push(lettersForKey[numForKey.indexOf(remainder)]);
- return toPush;
- }
- for (var l = 0; l < numForKey.length; l++) {
- if (remainder == numForKey[l] * 2) {
- toPush.push(lettersForKey[l]);
- toPush.push(lettersForKey[l]);
- } else if (remainder == numForKey[l] * 3) {
- toPush.push(lettersForKey[l]);
- toPush.push(lettersForKey[l]);
- toPush.push(lettersForKey[l]);
- }
- }
- return toPush;
- }
- numerals = numerals.join("");
- return numerals;
- }
Add Comment
Please, Sign In to add comment