Roman Numeral Converter way too long?

Is coding just not for me? I was looking at others peoples answers and they seem to have done this project way more simplified and clean. Kind of feeling foolish right now. Any advice on what to work on would be appreciated.

**Your code so far**
function convertToRoman(num) {
let myNum = num.toString().split("").map(Number)
let numToRoman ={1:"I",2:"II",3:"III",4:"IV",5:"V",6:"VI",7:"VII",8:"VIII",9:"IX",10:"X",20:"XX",30:"XXX",40:"XL",50:"L",60:"LX",70:"LXX",80:"LXXX",90:"XC",100:"C",200:"CC",300:"CCC",400:"CD",500:"D",600:"DC",700:"DCC",800:"DCCC",900:"CM", 1000:"M"}
let lastRoman={1:"M",2:"MM",3:"MMM",4:"MMMM",5:"MMMMM",6:"MMMMMM",7:"MMMMMMM",8:"MMMMMMMM",9:"MMMMMMMMM",10:"MMMMMMMMMM"}

  if(myNum.length ===2 && myNum[0] ===1){
  myNum[0] = numToRoman[10]}
  if(myNum[myNum.length-2] ===2 && myNum.length ===2){
  myNum[0] = numToRoman[10] + numToRoman[10]}
  if(myNum[myNum.length-2] ===3 && myNum.length ===2){
  myNum[0] = numToRoman[10] + numToRoman[10] + numToRoman[10]}
  if(myNum[myNum.length-2] ===4 && myNum.length ===2){
  myNum[0] = numToRoman[40]}
  if(myNum[myNum.length-2] ===5 && myNum.length ===2){
  myNum[0] = numToRoman[50]}
  if(myNum[myNum.length-2] ===6 && myNum.length ===2){
  myNum[0] = numToRoman[50] + numToRoman[10]}
  if(myNum[myNum.length-2] ===7 && myNum.length ===2){
  myNum[0] = numToRoman[50] + numToRoman[10] + numToRoman[10] }
  if(myNum[myNum.length-2] ===8 && myNum.length ===2){
  myNum[0] = numToRoman[50] + numToRoman[10] + numToRoman[10] + numToRoman[10] }
  if(myNum[myNum.length-2] ===9 && myNum.length ===2){
  myNum[0] = numToRoman[90]}

  if(myNum.length ===3 && myNum[0] ===1){
  myNum[0] = numToRoman[100]}
  if(myNum[myNum.length-3] ===2 && myNum.length <=3){
  myNum[0] = numToRoman[100] + numToRoman[100]}
  if(myNum[myNum.length-3] ===3 && myNum.length <=3){
  myNum[0] = numToRoman[100] + numToRoman[100]  + numToRoman[100]}
  if(myNum[myNum.length-3] ===4 && myNum.length <=3){
  myNum[0] = numToRoman[400]}
  if(myNum[myNum.length-3] ===5 && myNum.length <=3){
  myNum[0] = numToRoman[500]}
  if(myNum[myNum.length-3] ===6 && myNum.length <=3){
  myNum[0] = numToRoman[500] + numToRoman[100]}
  if(myNum[myNum.length-3] ===7 && myNum.length <=3){
  myNum[0] = numToRoman[500] + numToRoman[100] + numToRoman[100]}
  if(myNum[myNum.length-3] ===8 && myNum.length <=3){
  myNum[0] = numToRoman[500] + numToRoman[100] + numToRoman[100] + numToRoman[100]}
  if(myNum[myNum.length-3] ===9 && myNum.length <=3){
  myNum[0] = numToRoman[900]}
  

  
  

  if(myNum[myNum.length-3] ===2 && myNum.length ===4){
  myNum[1] = numToRoman[100] + numToRoman[100]}
  if(myNum[myNum.length-3] ===3 && myNum.length ===4){
  myNum[1] = numToRoman[100] + numToRoman[100]  + numToRoman[100]}
  if(myNum[myNum.length-3] ===4 && myNum.length ===4){
  myNum[1] = numToRoman[400]}
  if(myNum[myNum.length-3] ===5 && myNum.length ===4){
  myNum[1] = numToRoman[500]}
  if(myNum[myNum.length-3] ===6 && myNum.length ===4){
  myNum[1] = numToRoman[500] + numToRoman[100]}
  if(myNum[myNum.length-3] ===7 && myNum.length ===4){
  myNum[1] = numToRoman[500] + numToRoman[100] + numToRoman[100]}
  if(myNum[myNum.length-3] ===8 && myNum.length ===4){
  myNum[1] = numToRoman[500] + numToRoman[100] + numToRoman[100] + numToRoman[100]}
  if(myNum[myNum.length-3] ===9 && myNum.length ===4){
  myNum[1] = numToRoman[900]}
  if(myNum.length > 3){
    myNum[0] =lastRoman[myNum[0]]
  }

if(myNum[myNum.length-1] >=1 && myNum[myNum.length-1] <=9){
myNum[myNum.length-1] = numToRoman[[myNum[myNum.length-1]]]
}
for (let i =0; i<myNum.length; i++){
if(myNum[i] === 0){
myNum[i] = ""
}
}
if(myNum.length >2 && myNum[myNum.length-2] >=1 && myNum[myNum.length-2] <= 9){
myNum[myNum.length-2] = numToRoman[[myNum[myNum.length-2]*10]]
}
if(myNum.length >3 && myNum[myNum.length-3] >=1 && myNum[myNum.length-3] <= 9){
myNum[myNum.length-3] = numToRoman[[myNum[myNum.length-2]*100]]
}


return myNum.join("")
}

console.log(convertToRoman(44));
**Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Roman Numeral Converter

Link to the challenge:

Here is formatting to make it easier for other people to read your code

function convertToRoman(num) {
  let myNum = num.toString().split("").map(Number)
  let numToRoman = { 1: "I", 2: "II", 3: "III", 4: "IV", 5: "V", 6: "VI", 7: "VII", 8: "VIII", 9: "IX", 10: "X", 20: "XX", 30: "XXX", 40: "XL", 50: "L", 60: "LX", 70: "LXX", 80: "LXXX", 90: "XC", 100: "C", 200: "CC", 300: "CCC", 400: "CD", 500: "D", 600: "DC", 700: "DCC", 800: "DCCC", 900: "CM", 1000: "M" }
  let lastRoman = { 1: "M", 2: "MM", 3: "MMM", 4: "MMMM", 5: "MMMMM", 6: "MMMMMM", 7: "MMMMMMM", 8: "MMMMMMMM", 9: "MMMMMMMMM", 10: "MMMMMMMMMM" }

  if (myNum.length === 2 && myNum[0] === 1) {
    myNum[0] = numToRoman[10]
  }
  if (myNum[myNum.length - 2] === 2 && myNum.length === 2) {
    myNum[0] = numToRoman[10] + numToRoman[10]
  }
  if (myNum[myNum.length - 2] === 3 && myNum.length === 2) {
    myNum[0] = numToRoman[10] + numToRoman[10] + numToRoman[10]
  }
  if (myNum[myNum.length - 2] === 4 && myNum.length === 2) {
    myNum[0] = numToRoman[40]
  }
  if (myNum[myNum.length - 2] === 5 && myNum.length === 2) {
    myNum[0] = numToRoman[50]
  }
  if (myNum[myNum.length - 2] === 6 && myNum.length === 2) {
    myNum[0] = numToRoman[50] + numToRoman[10]
  }
  if (myNum[myNum.length - 2] === 7 && myNum.length === 2) {
    myNum[0] = numToRoman[50] + numToRoman[10] + numToRoman[10]
  }
  if (myNum[myNum.length - 2] === 8 && myNum.length === 2) {
    myNum[0] = numToRoman[50] + numToRoman[10] + numToRoman[10] + numToRoman[10]
  }
  if (myNum[myNum.length - 2] === 9 && myNum.length === 2) {
    myNum[0] = numToRoman[90]
  }

  if (myNum.length === 3 && myNum[0] === 1) {
    myNum[0] = numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 2 && myNum.length <= 3) {
    myNum[0] = numToRoman[100] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 3 && myNum.length <= 3) {
    myNum[0] = numToRoman[100] + numToRoman[100] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 4 && myNum.length <= 3) {
    myNum[0] = numToRoman[400]
  }
  if (myNum[myNum.length - 3] === 5 && myNum.length <= 3) {
    myNum[0] = numToRoman[500]
  }
  if (myNum[myNum.length - 3] === 6 && myNum.length <= 3) {
    myNum[0] = numToRoman[500] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 7 && myNum.length <= 3) {
    myNum[0] = numToRoman[500] + numToRoman[100] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 8 && myNum.length <= 3) {
    myNum[0] = numToRoman[500] + numToRoman[100] + numToRoman[100] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 9 && myNum.length <= 3) {
    myNum[0] = numToRoman[900]
  }





  if (myNum[myNum.length - 3] === 2 && myNum.length === 4) {
    myNum[1] = numToRoman[100] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 3 && myNum.length === 4) {
    myNum[1] = numToRoman[100] + numToRoman[100] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 4 && myNum.length === 4) {
    myNum[1] = numToRoman[400]
  }
  if (myNum[myNum.length - 3] === 5 && myNum.length === 4) {
    myNum[1] = numToRoman[500]
  }
  if (myNum[myNum.length - 3] === 6 && myNum.length === 4) {
    myNum[1] = numToRoman[500] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 7 && myNum.length === 4) {
    myNum[1] = numToRoman[500] + numToRoman[100] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 8 && myNum.length === 4) {
    myNum[1] = numToRoman[500] + numToRoman[100] + numToRoman[100] + numToRoman[100]
  }
  if (myNum[myNum.length - 3] === 9 && myNum.length === 4) {
    myNum[1] = numToRoman[900]
  }
  if (myNum.length > 3) {
    myNum[0] = lastRoman[myNum[0]]
  }

  if (myNum[myNum.length - 1] >= 1 && myNum[myNum.length - 1] <= 9) {
    myNum[myNum.length - 1] = numToRoman[[myNum[myNum.length - 1]]]
  }
  for (let i = 0; i < myNum.length; i++) {
    if (myNum[i] === 0) {
      myNum[i] = ""
    }
  }
  if (myNum.length > 2 && myNum[myNum.length - 2] >= 1 && myNum[myNum.length - 2] <= 9) {
    myNum[myNum.length - 2] = numToRoman[[myNum[myNum.length - 2] * 10]]
  }
  if (myNum.length > 3 && myNum[myNum.length - 3] >= 1 && myNum[myNum.length - 3] <= 9) {
    myNum[myNum.length - 3] = numToRoman[[myNum[myNum.length - 2] * 100]]
  }


  return myNum.join("")
}

console.log(convertToRoman(44));

Other people have a cleaner code because of their experience. Come back to this code after a month, year or so and you will find yourself thinking of a different and also a cleaner approach.

1 Like

you have hardcoded too much stuff

1 Like