Converts the given number from arabic to roman numeral system.
A script by V.
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 |