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;
}`