Hi All!
I finally got this one to work! It’s pretty bulky though lol. Could you provide some feedback as to how I could tidy this up without really giving me the answer? I’d like to refactor this and improve upon it, but really just be pointed in the right direction.
function convertToRoman(num) {
var numKey = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000];
var romanKey = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', 'M', 'MM', 'MMM'];
var convertNum = String(num);
var number = convertNum.split('');
var final = [];
if (number.length === 1) {
var one = number[0];
one = numKey.indexOf(Number(one));
one = romanKey[one];
final.push(one);
} else if (number.length === 2) {
var ten = number[0] + '0';
ten = numKey.indexOf(Number(ten));
ten = romanKey[ten];
final.push(ten);
var tenOne = number[1];
tenOne = numKey.indexOf(Number(tenOne));
tenOne = romanKey[tenOne];
final.push(tenOne);
} else if (number.length === 3) {
var hundo = number[0] + '0' + '0';
hundo = numKey.indexOf(Number(hundo));
hundo = romanKey[hundo];
final.push(hundo);
var hundoTen = number[1] + '0';
hundoTen = numKey.indexOf(Number(hundoTen));
hundoTen = romanKey[hundoTen];
final.push(hundoTen);
var hundoTenOne = number[2];
hundoTenOne = numKey.indexOf(Number(hundoTenOne));
hundoTenOne = romanKey[hundoTenOne];
final.push(hundoTenOne);
} else if (number.length === 4) {
var thou = number[0] + '0' + '0' + '0';
thou = numKey.indexOf(Number(thou));
thou = romanKey[thou];
final.push(thou);
var thouHundo = number[1] + '0' + '0';
thouHundo = numKey.indexOf(Number(thouHundo));
thouHundo = romanKey[thouHundo];
final.push(thouHundo);
var thouHundoTen = number[2] + '0';
thouHundoTen = numKey.indexOf(Number(thouHundoTen));
thouHundoTen = romanKey[thouHundoTen];
final.push(thouHundoTen);
var thouHundoTenOne = number[3];
thouHundoTenOne = numKey.indexOf(Number(thouHundoTenOne));
thouHundoTenOne = romanKey[thouHundoTenOne];
final.push(thouHundoTenOne);
}
return final.join('');
}
convertToRoman(36);
I feel like I should be using a loop for each iteration of the number especially in the thousands since I’m doing number 0,1,2,3. I’m just really confused about how to make it a number that would be able to be found in my initial arrays, but maybe I’m just thinking about it wrong in general.
Thanks,
Seth