Roman Numeral Converter

Hi everyone,

I believed I understood roman numeral convert but when I execute the code freeCodeCamp doesn’t accept it as a solution? :no_mouth:
why 39999 invalid number for Roman numeral conversion

const roman = {
  3000:"MMM",
  2000:"MM",
  1000: "M",
  900: "CM",
  500: "D",
  400: "CD",
  100: "C",
  90: "XC",
  50: "L",
  40: "XL",
  10: "X",
  9: "IX",
  5: "V",
  4: "IV",
  3:"III",
  2:"II",
  1: "I",
};

let keyArr = Object.keys(roman);
let strNum = "";

function convertToRoman(num) {
  if (num === 0) {
    return "";
  }
  for (let i = keyArr.length - 1; i >= 0; i--) {
    while (num >= keyArr[i]) {
      strNum += roman[keyArr[i]];
      num -= keyArr[i];
      if (num === 0) {
        break;
      }
    }
    if (num === 0) {
      break;
    }
  }
  return strNum;
}
console.log(convertToRoman(4));

Welcome @reembirawi, after a long time!

Your code is successfully passing the tests and is indeed correct.

You can’t write more than three consecutive Roman numbers. Also, the traditional Roman numeral system has no symbols like dash-over letters. This means you cannot visualize a number greater than 3999 within the traditional Roman numeral system.

In conclusion, If I understood correctly, It is enough for your code to work perfectly in the traditional Roman numeral system for the project.

Happy coding!

thank you @femincan
but this is what happens when I run the code and I can’t understand the problem.

the test is testing your:

function convertToRoman(num){

}

You need to include all global variable inside it.

1 Like

I have forgotten to mention, just remove the console.log statement at the bottom.

ooh! :saluting_face:

thank you @WongYC-66

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