function convertToRoman(num) {
var chart = {
0:'',
1:'I',
2:'II',
3:'III',
4:'IV',
5:'V',
6:'VI',
7:'VII',
8:'VIII',
9:'IX',
10:'X',
20:'XX',
30:'XXX',
40:'XL',
50:'L',
60:'LX',
70:'LXX',
80:'LXXX',
90:'XC',
100:'C',
200:'CC',
300:'CCC',
400:'CD',
500:'D',
600:'DC',
700:'DCC',
800:'DCCC',
900:'CM',
1000:'M',
2000:'MM',
3000:'MMM'
};
var numString = num.toString().split('');
if(numString.length == 1){
return chart[parseInt(numString[0])];
} else if(numString.length == 2){
return chart[parseInt(numString[0])*10]+chart[parseInt(numString[1])];
} else if(numString.length == 3){
return chart[parseInt(numString[0])*100]+
chart[parseInt(numString[1])*10]+
chart[parseInt(numString[2])];
} else if(numString.length == 4){
return chart[parseInt(numString[0])*1000]+
chart[parseInt(numString[1])*100]+
chart[parseInt(numString[2])*10]+
chart[parseInt(numString[3])];
}
}
convertToRoman(2);
No comments:
Post a Comment