This was my solution to the challenge, but I also tried to make it into an actual web-page. Then I realized that it’s only converting correct up to 3999. So I ended up doing a bit more coding to get a real converter. Link to Github at the bottom. Still not sure if it’s correct though…
function convertToRoman(num) {
numString = num.toString();
numArray = numString.split("");
var positionOne = ["", “I”, “II”, “III”, “IV”, “V”, “VI”, “VII”, “VIII”, “IX”];
var positionTen = ["", “X”, “XX”, “XXX”, “XL”, “L”, “LX”, “LXX”, “LXXX”, “XC”];
var positionHundred = ["", “C”, “CC”, “CCC”, “CD”, “D”, “DC”, “DCC”, “DCCC”, “CM”];
var positionThousand = ["", “M”, “MM”, “MMM”, “MV”, “V”, “VI”, “VII”, “VIII”, “IX”];
if (numArray.length === 1) {
return positionOne[numArray];
} else if (numArray.length === 2) {
return positionTen[numArray[0]] + positionOne[numArray[1]];
} else if (numArray.length === 3) {
return positionHundred[numArray[0]] + positionTen[numArray[1]] + positionOne[numArray[2]];
} else if (numArray.length === 4) {
return positionThousand[numArray[0]] + positionHundred[numArray[1]] + positionTen[numArray[2]] + positionOne[numArray[3]];
}
}