Edit: Here is the challenge with my solution:
It is working on 4 digits numbers or less.
Hello guys!
I tried with just one digit numbers, and I dont get it why it is not working.
Please give me some hint.
function convertToRoman(num) {
var roman="";
if(num.toString().length==4){
switch(num.toString().charAt(0)){
case “1”:
roman+=“M”;
break;
case “2”:
roman+=“MM”;
break;
case “3”:
roman+=“MMM”;
break;
}
switch(num.toString().charAt(1)){
case “0”:
roman+="";
break;
case “1”:
roman+=“C”;
break;
case “2”:
roman+=“CC”;
break;
case “3”:
roman+=“CCC”;
break;
case “4”:
roman+=“CD”;
break;
case “5”:
roman+=“D”;
break;
case “6”:
roman+=“DC”;
break;
case “7”:
roman+=“DCC”;
break;
case “8”:
roman+=“DCCC”;
break;
case “9”:
roman+=“CM”;
break;
}
switch(num.toString().charAt(2)){
case “0”:
roman+="";
break;
case “1”:
roman+=“X”;
break;
case “2”:
roman+=“XX”;
break;
case “3”:
roman+=“XXX”;
break;
case “4”:
roman+=“XL”;
break;
case “5”:
roman+=“L”;
break;
case “6”:
roman+=“LX”;
break;
case “7”:
roman+=“LXX”;
break;
case “8”:
roman+=“LXXX”;
break;
case “9”:
roman+=“XC”;
break;
}switch(num.toString().charAt(3)){
case “0”:
roman+="";
break;
case “1”:
roman+=“I”;
break;
case “2”:
roman+=“II”;
break;
case “3”:
roman+=“III”;
break;
case “4”:
roman+=“IV”;
break;
case “5”:
roman+=“V”;
break;
case “6”:
roman+=“VI”;
break;
case “7”:
roman+=“VII”;
break;
case “8”:
roman+=“VIII”;
break;
case “9”:
roman+=“IX”;
break;
}}
else if(num.toString().length==3){
switch(num.toString().charAt(0)){
case "0":
roman+="";
break;
case "1":
roman+="C";
break;
case "2":
roman+="CC";
break;
case "3":
roman+="CCC";
break;
case "4":
roman+="CD";
break;
case "5":
roman+="D";
break;
case "6":
roman+="DC";
break;
case "7":
roman+="DCC";
break;
case "8":
roman+="DCCC";
break;
case "9":
roman+="CM";
break;
}
switch(num.toString().charAt(1)){
case "0":
roman+="";
break;
case "1":
roman+="X";
break;
case "2":
roman+="XX";
break;
case "3":
roman+="XXX";
break;
case "4":
roman+="XL";
break;
case "5":
roman+="L";
break;
case "6":
roman+="LX";
break;
case "7":
roman+="LXX";
break;
case "8":
roman+="LXXX";
break;
case "9":
roman+="XC";
break;
}switch(num.toString().charAt(2)){
case "0":
roman+="";
break;
case "1":
roman+="I";
break;
case "2":
roman+="II";
break;
case "3":
roman+="III";
break;
case "4":
roman+="IV";
break;
case "5":
roman+="V";
break;
case "6":
roman+="VI";
break;
case "7":
roman+="VII";
break;
case "8":
roman+="VIII";
break;
case "9":
roman+="IX";
break;
}
}
else if(num.toString().length==2){
switch(num.toString().charAt(0)){
case "0":
roman+="";
break;
case "1":
roman+="X";
break;
case "2":
roman+="XX";
break;
case "3":
roman+="XXX";
break;
case "4":
roman+="XL";
break;
case "5":
roman+="L";
break;
case "6":
roman+="LX";
break;
case "7":
roman+="LXX";
break;
case "8":
roman+="LXXX";
break;
case "9":
roman+="XC";
break;
}switch(num.toString().charAt(1)){
case "0":
roman+="";
break;
case "1":
roman+="I";
break;
case "2":
roman+="II";
break;
case "3":
roman+="III";
break;
case "4":
roman+="IV";
break;
case "5":
roman+="V";
break;
case "6":
roman+="VI";
break;
case "7":
roman+="VII";
break;
case "8":
roman+="VIII";
break;
case "9":
roman+="IX";
break;
}
}
else if(num.toString().length==1){
switch(num.toString().charAt(0)){
case "0":
roman+="";
break;
case "1":
roman+="I";
break;
case "2":
roman+="II";
break;
case "3":
roman+="III";
break;
case "4":
roman+="IV";
break;
case "5":
roman+="V";
break;
case "6":
roman+="VI";
break;
case "7":
roman+="VII";
break;
case "8":
roman+="VIII";
break;
case "9":
roman+="IX";
break;
}
}
return roman;
}