Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 16:01
Show Gist options
  • Save rfprod/95c5601a37c2668ed2cc to your computer and use it in GitHub Desktop.
Save rfprod/95c5601a37c2668ed2cc to your computer and use it in GitHub Desktop.
Arabic to Roman Numeral Coverter
function arabicToRomanNumeralCoverter(num) {
if (number === 0) { return ''; }
const thousands = Math.floor(number / 1000);
const units = number % 10;
const tenths = number % 100 - units;
const hundreds = number % 1000 - tenths - units;
let inputArr = [thousands, hundreds/100, tenths/10, units];
const romanTenSeq = ["","I","II","III","IV","V","VI","VII","VIII","IX","X"];
const romanFifty = "L";
const romanHundred = "C";
const romanFiveHundred = "D";
const romanThousand = "M";
let output = '';
// thousands
while (inputArr[0] > 0) {
inputArr[0]--;
output += romanThousand;
}
// hundreds
while (inputArr[1] > 0) {
if (inputArr[1] === 9) {
inputArr[1] -= 9;
output += romanHundred + romanThousand;
} else if (inputArr[1] >= 5) {
inputArr[1] -= 5;
output += romanFiveHundred;
} else if (inputArr[1] === 4) {
inputArr[1] -= 4;
output += romanHundred + romanFiveHundred;
} else {
inputArr[1]--;
output += romanHundred;
}
}
// tenths
while (inputArr[2] > 0) {
if (inputArr[2] === 9) {
inputArr[2] -= 9;
output += romanTenSeq[10] + romanHundred;
} else if (inputArr[2] >= 5) {
inputArr[2] -= 5;
output += romanFifty;
} else if (inputArr[2] === 4) {
inputArr[2] -= 4;
output += romanTenSeq[10] + romanFifty;
} else {
inputArr[2]--;
output += romanTenSeq[10];
}
}
// units
while (inputArr[3] > 0) {
if (inputArr[3] === 9) {
inputArr[3] -= 9;
output += romanTenSeq[1] + romanTenSeq[10];
} else if (inputArr[3] >= 5) {
inputArr[3] -= 5;
output += romanTenSeq[5];
} else if (inputArr[3] === 4) {
inputArr[3] -= 4;
output += romanTenSeq[1] + romanTenSeq[5];
} else {
inputArr[3]--;
output += romanTenSeq[1];
}
}
return output;
}
arabicToRomanNumeralCoverter(1000); // M

Arabic to Roman Numeral Converter

Converts the given number from arabic to roman numeral system.

A script by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment