I believed I understood roman numeral convert but when I execute the code freeCodeCamp doesn’t accept it as a solution?
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));
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.