Roman Numeral Converter: Can I improve it?

Hello,
Roman Numeral Converter
I finished this project :wink:
I hope it’s fine for you.
I post this message in order to have your good advices to improve the code before I go to see the FCC solutions.

const decomposer = {
  0:[0],
  1:[1],
  2:[1,1],
  3:[1,1,1],
  4:[1,5],
  5:[5],
  6:[5,1],
  7:[5,1,1],
  8:[5,1,1,1],
  9:[1,10]
}
const roman = {
  0:"",
  1:"I",
  5:"V",
  10:"X",
  50:"L",
  100:"C",
  500:"D",
  1000:"M"
}

function convertToRoman(num) {
 let numDecomposed = []

 /* I transform the number placed in argument in "string" to make an array decomposed by each digit that composes it. */
 let numToBreakDown = num.toString().split("")
 for (let i = 0; i < numToBreakDown.length; i++) {
   numDecomposed.push(
    
  /*I match each value of the array with its value in the "decomposer" object and then 
  express each digit of this value by its representation in thousand, hundred or unit using 
 'numToBreakDown.length-1 - i':
   336 : [[100,100,100],[10,10,10],[5,1]]
   501 : [[500],[0],[1]]
  */
    decomposer[numToBreakDown[i]].map(item => item * Math.pow(10, numToBreakDown.length-1 - i))
    /* I match each element of the tables by their value in the "roman" object.
    336 : [["C","C","C"],["X","X","X"],["V","I"]]
    501 : [["D"],["0"],["1"]]
     */
   .map(item => roman[item]))
 }
/* I flatten the arrays with flat():
    Pour 336 : ["C","C","C","X","X","X","V","I"]
    Pour 501 : ["D","","I"]
   Then I transform into "string" and return the result:
    Pour 336 : "CCCXXXVI"
    Pour 501 : "DI"
*/
return numDecomposed.flat().join('')
}
convertToRoman(501);

I prod of me :smiley:
Is it good ?

1 Like

Apparently I can’t put comments wher I want, so I put the explanations at the beginning even if I know you don’t need them:

const decomposer = {
  0:[0],
  1:[1],
  2:[1,1],
  3:[1,1,1],
  4:[1,5],
  5:[5],
  6:[5,1],
  7:[5,1,1],
  8:[5,1,1,1],
  9:[1,10]
}
const roman = {
  0:"",
  1:"I",
  5:"V",
  10:"X",
  50:"L",
  100:"C",
  500:"D",
  1000:"M"
}
/* I transform the number placed in argument in "string" to make an array decomposed by each digit that composes it.
*/
/* I "match" each value of the array with its value in the "decomposer" object and then express each digit of this value by its representation in thousand, hundred or unit using 'numToBreakDown.length-1 - i':
     336 : [[100,100,100],[10,10,10],[5,1]]
     501 : [[500],[0],[1]]
     */
 /* I match each element of the tables by their value in the "roman" object.
    336 : [["C","C","C"],["X","X","X"],["V","I"]]
    501 : [["D"],["0"],["1"]]
     */
/* I flatten the arrays with flat():
    Pour 336 : ["C","C","C","X","X","X","V","I"]
    Pour 501 : ["D","","I"]
   Then I transform into "string" and return the result:
    Pour 336 : "CCCXXXVI"
    Pour 501 : "DI"
*/
function convertToRoman(num) {
 let numDecomposed = []
 let numToBreakDown = num.toString().split("")
 for (let i = 0; i < numToBreakDown.length; i++) {
   numDecomposed.push(decomposer[numToBreakDown[i]].map(item => item * Math.pow(10, numToBreakDown.length-1 - i))
   .map(item => roman[item]))
 }
return numDecomposed.flat().join('')
}
convertToRoman(501);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.