JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

i ran the code in vscoe and leetcode and it worked well at both but when i run it in FCC i am facing (undifiened) error , is there a mistake in the way i return my function? or is it something else, thanks in adnvance

function convertToRoman(num) {
  result = "";
  while (num > 0) {
    if (num >= 1000) {
      result += "M";
      num -= 1000;
    } else if (900 <= num && num < 1000) {
      result += "CM";
      num -= 900;
    } else if (500 <= num && num < 900) {
      result += "D";
      num -= 500;
    } else if (400 <= num && num < 500) {
      result += "CD";
      num -= 400;
    } else if (100 <= num && num < 400) {
      result += "C";
      num -= 100;
    } else if (90 <= num && num < 100) {
      result += "XC";
      num -= 90;
    } else if (50 <= num && num < 90) {
      result += "L";
      num -= 50;
    } else if (40 <= num && num < 50) {
      result += "XL";
      num -= 40;
    } else if (10 <= num && num < 40) {
      result += "X";
      num -= 10;
    } else if (num === 9) {
      result += "IX";
      num -= 9;
    } else if (5 <= num && num < 9) {
      result += "V";
      num -= 5;
    } else if (num === 4) {
      result += "IV";
      num -= 4;
    } else if (1 <= num && num < 4) {
      result += "I";
      num -= 1;
    }
  }
  num = result
  return num.toUpperCase();
}

Your browser information:

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

Challenge Information:

JavaScript Algorithms and Data Structures Projects - Roman Numeral Converter

Hi there and welcome to our community!

Strictly, you should always declare your variables using let or const.
Whilst this may not always throw an error, it will do so in ‘strict mode’, which is why you have an undefined error for result.

Other than that, your code should work just fine.

2 Likes

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