A Pen by Robert Van Housen on CodePen.
Created
March 10, 2018 16:46
-
-
Save Javabob61/479e9f2d97197154f3c912f7cba767a6 to your computer and use it in GitHub Desktop.
Integers to roman rumeral converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convertToRoman(num) { | |
// noprotect | |
var romeNum = []; | |
var mCount = 0; | |
var cCount = 0; | |
var iCount = 0; | |
var xCount = 0; | |
if (num >= 1000) { /////// Multiples of 1000 | |
mCount = parseInt(num/1000); | |
cCount = parseInt((num - mCount*1000)/100); | |
xCount = parseInt((num - (mCount*1000) - (cCount*100))/10); | |
iCount = parseInt(num - mCount*1000 - cCount*100 - xCount*10); | |
} | |
else{ | |
cCount = parseInt(num/100); | |
xCount = parseInt((num - cCount*100)/10); | |
iCount = parseInt(num - cCount*100 - xCount*10); | |
} | |
if (mCount >= 1) { | |
for (var i = 0 ; i < mCount; i++) { | |
romeNum.push("M"); | |
} | |
} | |
if (cCount >= 9) { ////// Greater or equal to 900 | |
romeNum.push("CM"); | |
} | |
if (cCount >= 8 && cCount < 9) { | |
romeNum.push("DCCC"); | |
} | |
if (cCount >= 7 && cCount < 8) { | |
romeNum.push("DCC"); | |
} | |
if (cCount >= 6 && cCount < 7) { ////// Between 600 and 700 | |
romeNum.push("DC"); | |
} | |
if (cCount >= 5 && cCount < 6) { ////// Between 500 and 600 | |
romeNum.push("D"); | |
} | |
if (cCount < 5 && cCount >= 4) { //// Between 400 and 500 | |
romeNum.push("CD"); | |
} | |
if (cCount < 4) { | |
for (var k = 0; k < cCount; k++){ | |
romeNum.push("C"); | |
} | |
} | |
if (xCount >= 9 ) { | |
romeNum.push("XC"); | |
} | |
if (xCount >= 8 && xCount < 9 ) { | |
romeNum.push("LXXX"); | |
} | |
if (xCount >= 7 && xCount < 8 ) { | |
romeNum.push("LXX"); | |
} | |
if (xCount >= 6 && xCount < 7 ) { | |
romeNum.push("LX"); | |
} | |
if (xCount >= 5 && xCount < 6 ) { | |
romeNum.push("L"); | |
} | |
if (xCount >= 4 && xCount < 5) { // between 40 and 50 | |
romeNum.push("XL"); | |
} | |
if (xCount < 4 ) { | |
for (var l = 0; l < xCount; l++){ | |
romeNum.push("X"); | |
} | |
} | |
if (iCount === 9) { | |
romeNum.push("IX"); | |
} | |
if (iCount === 8) { | |
romeNum.push("VIII"); | |
} | |
if (iCount === 7) { | |
romeNum.push("VII"); | |
} | |
if (iCount === 6) { | |
romeNum.push("VI"); | |
} | |
if (iCount === 5) { | |
romeNum.push("V"); | |
} | |
if (iCount === 4) { | |
romeNum.push("IV"); | |
} | |
if (iCount < 4) { | |
for (var m = 0; m < iCount; m++) { | |
romeNum.push("I"); | |
} | |
} | |
console.log(romeNum.join("")); | |
} | |
convertToRoman(3999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment