Roman Numeral Converter - Criticism/Tips wanted

Roman Numeral Converter - Criticism/Tips wanted
0.0 0

#1

This code worked but, it looked nothing like the solutions in the forum. If anyone wants to look it over and give me any constructive criticism I’d more than appreciate it.

function convertToRoman(num) {
  const roman = ["I", "IV","V", "IX", "X", "XL", "L", "XC", "C", "CD", "D",  "CM", "M"];
  const deci  = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
  
   function numLengthCalc(a){ 
             return a.toString().length;
              }
  const numLength = numLengthCalc(num);
  
  
  function breakUp(num){
    
      
     function divisorArray(numberOfDigits){
      
    switch(numberOfDigits){
      case 1:
        return [1];
      
      case 2:
        return [10, 1];
      
      case 3:
        return [100, 10, 1];
      
      case 4:
        return [1000, 100, 10, 1];
        
      default:
        return "hello";
    }
    
  }
    
    
      var aaa = num.toString().split("");
      var length = aaa.length;
      var divArray = divisorArray(length);
    
    var bbb=[];
    
    for(var i=0; i<aaa.length; i++){
        
          bbb.push(aaa[i]*divArray[i]);
        
      }
    
    
    
    
    return bbb;
    
  }
 
    function compare(a,b){
      
    return a-b;
    
  }
  
  function numeralFinder(a, arr){
     deci.push(a);
                let indexOfFirst = deci.sort(compare).indexOf(a)-1;
                let howMany = a/deci[indexOfFirst];
                for(let i=0; i<howMany; i++){
                
                  arr.push(roman[indexOfFirst]);
               
                }
               deci.splice((indexOfFirst+1), 1);
  }

  
  if(deci.includes(num)){
    return roman[deci.indexOf(num)];
    
  } else{
    
      var a = breakUp(num);
      
    
    let numeralArray=[];
         a.forEach(number=>{
           
          if(deci.includes(number)){
        
             numeralArray.push(roman[deci.indexOf(number)]);
    
          }else if(number >1000){
               numeralFinder(number, numeralArray);
                
          }else if(number >500){
              let howMany = Math.floor(number-500)/100;
            
                 
              numeralArray.push("D");
            
             for(let i=0; i<howMany; i++){
                   numeralArray.push("C");
                 }
            
            
            
          }else if(number >50){
              let howMany = Math.floor(number-50)/10;
            
                 
              numeralArray.push("L");
            
             for(let i=0; i<howMany; i++){
                   numeralArray.push("X");
                 }
                      
          } else if(number>=10){
               numeralFinder(number, numeralArray);
            
          } else if(number <5){
                    
                numeralFinder(number, numeralArray);
                    
          } else if( number > 5){
                var howManyI = number -5;
                numeralArray.push("V");
                     
                for(let i=0; i<howManyI; i++){
                    numeralArray.push("I");
                 }
                     
              }
              
        });
   
     return numeralArray.join("");
  }
  
  
//  return deci.sort(compare).indexOf(num);
  
//   return breakUp(num);
}

convertToRoman(36);

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.