My Ugly Solution to Roman Numeral Converter-feedbacks please

My Ugly Solution to Roman Numeral Converter-feedbacks please


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="";  
       var k=Math.floor(num/div);

     if(k<4 && k!=0){
     for(var j=0;j<k;j++)
 else if(k==4){
 else if(k==5){
 else if(k<9 && k!=0){
     for(var j=0;j<k-5;j++)
 else if(k==9){

   return roman;


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 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"


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.