# My Ugly Solution to Roman Numeral Converter-feedbacks please

My Ugly Solution to Roman Numeral Converter-feedbacks please
0

#1

After a lot frustration and disappointment the first day for three hours, the next day it took me 40 minutes to solve this challenge. Although it works up to 1399. My code looks terrible imho, it is bulky. How would you change this code and make it more elegant?

``````function convertToRoman(num) {
var objBase={1:"I",5:"V",10:"X",50:"L",100:"C",500:"D",1000:"M"};
var arrDiv=[1000,100,10,1];
var arrK=[];
var roman="";
arrDiv.forEach(function(div){
var k=Math.floor(num/div);
num=num-k*div;
arrK.push(k);
});

arrK.forEach(function(k,i){
if(k<4 && k!=0){
for(var j=0;j<k;j++)
roman+=objBase[arrDiv[i]];
}
else if(k==4){
roman+=objBase[arrDiv[i]]+objBase[arrDiv[i]*5];
}
else if(k==5){
roman+=objBase[arrDiv[i]*5];
}
else if(k<9 && k!=0){
roman+=objBase[arrDiv[i]*5];
for(var j=0;j<k-5;j++)
roman+=objBase[arrDiv[i]];
}
else if(k==9){
roman+=objBase[arrDiv[i]]+objBase[arrDiv[i-1]];
}
});

return roman;
}```````

#2

I’ve seen a few solutions for this sort of question around. The more elegant solutions seem to bite the bullet and use an `objBase` with more key/value pairs. Would that help? For instance, on exercism.io I used:

``````    var romans = {
1000: "M",
900: "CM",
500: "D",
400: "CD",
100: "C",
90: "XC",
50: "L",
40: "XL",
10: "X",
9: "IX",
5: "V",
4: "IV",
1: "I"
}
``````

#3

Thanks for taking time to respond @sa-mm, yes I saw smth similar on Algorithm Challenge Guide too. But somehow implementing logic of say CM seemed better to me. The problem is that it is me who solved the challenge without any external help. But when it comes to reviewing it later I dont understand much what the heck is going on, so wanted to make it more readable without dismissing logic side.