Roman Numeral Converter

I recently finished the Roman Numeral Converter in the JavaScript Algorithms and Data Structures Projects challenges .please let me know how can I simplify it in future .any feedback is appreciated. thanks in advance. :pray: :pray:

function convertToRoman(num) {
  let romanNumeral = "";

  while (num > 0) {
    if (num < 4) {
      romanNumeral += "I";
      num -= 1;
    } else if (num ==4) {
      romanNumeral += "IV";
      num -= 4;
    }
    else if (num >= 5 && num < 9) {
      romanNumeral += "V";
      num -= 5;
    }
    else  if (num === 9)  {
      romanNumeral += "IX";
      num -= 9;

    }else  if (num >= 10 && num < 40)  {
      romanNumeral += "X";
      num -= 10;

 }
 else  if (num >= 40 && num < 50)  {
      romanNumeral += "XL";
      num -= 40;

 }
 else  if (num >= 50 && num < 90)  {
      romanNumeral += "L";
      num -= 50;

 }
 
 else  if (num >= 90 && num < 100)  {
      romanNumeral += "XC";
      num -= 90;

 }
 else  if (num >= 100 && num < 400)  {
      romanNumeral += "C";
      num -= 100;

 }
 else  if (num >= 400 && num < 500)  {
      romanNumeral += "CD";
      num -= 400;

 }
 else  if (num >= 500 && num < 900)  {
      romanNumeral += "D";
      num -= 500;

 }
 else  if (num >= 900 && num < 1000)  {
      romanNumeral += "CM";
      num -= 900;

 }
  else  if (num >= 1000 )  {
      romanNumeral += "M";
      num -= 1000;

 }else{
   romanNumeral += "_";
      num -= 1;
    }
  }
      

 return romanNumeral;
}

console.log(convertToRoman(3999));
1 Like

For all the else/if statements that have two conditions in them, do you really need the first one? For example:

Is it really possible that num could be less than 5 at this point?

Also, I don’t see the purpose of the last else statement.

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