Hi guys, I just did the Roman Numeral Converter Challenge and wanted to share my solution with you! I know is a bit messy but I am so glad that I finally solved it! Also I would like if you share others solutions so I can improve this one!
var romans = ["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 decimals = [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];
var numberLength = num.toString().length;
var numArr = num.toString(10).split("");
var data = [];
if(decimals.indexOf(num)!==-1){
return romans[decimals.indexOf(num)];
}
for(var i = 0; i <numberLength; i++){
if(numberLength==4 && i === 0 && numArr[i]!==0){
numArr[i]= numArr[i]+"000";
data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
}else if((numberLength==4 ) && i === 1 && numArr[i]!==0){
numArr[i]= numArr[i]+"00";
data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
}else if((numberLength==4 || numberLength===2) && i === 2 && numArr[i]!==0){
numArr[i]= numArr[i]+"0";
data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
}else if((numberLength===2) && i === 0 && numArr[i]!==0){
numArr[i]= numArr[i]+"0";
data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
}else if((numberLength===3) && i === 0 && numArr[i]!==0){
numArr[i]= numArr[i]+"00";
data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
}else if((numberLength===3) && i === 1 && numArr[i]!==0){
numArr[i]= numArr[i]+"0";
data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
}
else{
data.push(romans[decimals.indexOf(parseInt(numArr[i]))]);
}
}
return data.join("");