Looking for roman numeral converter feedback

Here is my solution to the following for credit problem on the Data structure and array course. I am just wondering if this code is considered super inefficient. I feel like a switch can be implemented somehow, but I opted for the logic statements.

Any feedback?



function convertToRoman(num) {

// Declare string to store numerals
  let romanNum = ""
// Set variable to temporarily hold a value in a for/while loop set to push an amount of roman numerals to string
  let pushNum = 0
// Set variable to pass remaining quantity of the original number through the program to be assigned roman numerals
  let passRemainder = num


/* Archtype of the "switch" set up
  If statement qualifys the number to enter the brackets of corresponding roman numeral values
  pushNum is assigned the truncated value of the number divided by a roman numeral's value
  While loop pushes the specified amount of numerals to the romanNum string
  passRemainder is set to the number minus the quantity of roman numerals assigned to the romanNum string and passed on to the next qualifying if statement
*/

  if(Math.trunc(passRemainder / 1000) > 0){
    pushNum = Math.trunc(passRemainder / 1000)

    while(pushNum > 0){
      romanNum = romanNum + "M"
      pushNum--
    }
  passRemainder %= 1000
  }

  if(Math.trunc(passRemainder / 900 >0)){
    pushNum = Math.trunc(passRemainder / 900)
    
    while(pushNum > 0){
      romanNum = romanNum + "CM"
      pushNum--

    }
    passRemainder %= 900
  }

if(Math.trunc(passRemainder / 500 > 0)){
    pushNum = Math.trunc(passRemainder / 500)
    
  while(pushNum > 0){
    romanNum = romanNum + "D"
    pushNum--

    }
    passRemainder %= 500
  }

if(Math.trunc(passRemainder / 400 > 0)){
  pushNum = Math.trunc(passRemainder / 400)
    
  while(pushNum > 0){
    romanNum = romanNum + "CD"
    pushNum--

    }
    passRemainder %= 400
  }

if(Math.trunc(passRemainder / 100 > 0)){
  pushNum = Math.trunc(passRemainder / 100)
    
  while(pushNum > 0){
    romanNum = romanNum + "C"
    pushNum--

    }
    passRemainder %= 100
  }

if(Math.trunc(passRemainder / 90 > 0)){
pushNum = Math.trunc(passRemainder / 90)
    
  while(pushNum > 0){
    romanNum = romanNum + "XC"
    pushNum--

    }
    passRemainder %= 90
  }

if(Math.trunc(passRemainder / 50 > 0)){
pushNum = Math.trunc(passRemainder / 50)
    
  while(pushNum > 0){
    romanNum = romanNum + "L"
    pushNum--

    }
    passRemainder %= 50
  }

if(Math.trunc(passRemainder / 40 > 0)){
pushNum = Math.trunc(passRemainder / 40)
    
  while(pushNum > 0){
    romanNum = romanNum + "XL"
    pushNum--

    }
    passRemainder %= 40
  }

if(Math.trunc(passRemainder / 10 > 0)){
pushNum = Math.trunc(passRemainder / 10)
    
  while(pushNum > 0){
    romanNum = romanNum + "X"
    pushNum--

    }
    passRemainder %= 10
  }

if(Math.trunc(passRemainder / 9 > 0)){
pushNum = Math.trunc(passRemainder / 9)
    
  while(pushNum > 0){
    romanNum = romanNum + "IX"
    pushNum--

    }
    passRemainder %= 9
  }

if(Math.trunc(passRemainder / 5 > 0)){
pushNum = Math.trunc(passRemainder /5)
    
  while(pushNum > 0){
    romanNum = romanNum + "V"
    pushNum--

    }
    passRemainder %= 5
  }

if(Math.trunc(passRemainder / 4 > 0)){
pushNum = Math.trunc(passRemainder /4)
    
  while(pushNum > 0){
    romanNum = romanNum + "IV"
    pushNum--

    }
    passRemainder %= 4
  }

if(Math.trunc(passRemainder / 1 > 0)){
pushNum = Math.trunc(passRemainder /1)
    
  while(pushNum > 0){
    romanNum = romanNum + "I"
    pushNum--

    }
  }

  return romanNum;
}

Good job getting it passing!

You have a lot of repeated logic. I would look for a way to decrease this repetition.

Thanks! Yes, I modeled a switch but built out a logic structure to serve as a “case.” then copied and pasted the structure, passing the variable through each “case” so to speak. I will have to think on a better way to decrease repetitive logic, as I figured that would be considered inefficient. When something dawns on me I will update.

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