Help Needed on Roman Numeral Converter Project

I’m working on Roman numeral converter project.

My code

 function convertToRoman(num) {
var thou = ["","M", "MM", "MMM", "MMMM"];
var hund = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM "];
var tens = ["",  "X", "XX", "XXX", "XL", "L",            "LX", "LXX", "LXXX", "XC"];                                          
var ones = ["", "I", "II", "III", "IV", "V", "VI","VII","VIII","IX"];                                                           
var t = thou[Math.floor(num/1000)]; 
var h = hund[Math.floor((num%1000)/100)];
var te = tens[Math.floor((num%100)/10)];
var on = ones[Math.floor(num%10)];
  var rom = t + h + te + on ;      
 return rom;


My code is returning correct for every condition except the last one(3999). I don’t know what’s the problem is. Please help.

what code is used for he thousands?

var t = thou[Math.floor(num/1000)];

I don’t know why its not showing on my post. Its present there.

there was a line break missing, I edited your post to add it in

let me check again your code

your output is MMMCM XCIX, there is an extra space, where could it come from?

1 Like

Thank you so much. I corrected it. Now it works fine.

next time you can add yourself the function to the editor and see what’s the output :slight_smile:

1 Like

Sure I will do. Thanks for the help. :grinning: