Roman numeral converter. toString problem

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;

}

  1. You need to call the toString method in your if statment.

You have:
num.toString.length==1
Should be:
num.toString().length==1

  1. I’m not sure what you are trying to do with charAt(). When your if statement condition is true you know you have a one digit number so you can just call num.toString()

switch(num.toString())

It is a project in the Curriculum: JavaScript Algorithms and Data Structures Projects: Roman Numeral Converter.
So the details: convert any 4 digits or less numeric numbers to roman and thats my first argument for the last character just try if its working. toString without parenthesis is mispelled, with toString() still not working. Thats why I used charAt(), because I want to convert the first digit, second, third and fourth separately.

If it is not understandable, I will write my whole code plan tomorrow, and edit my post :slight_smile:

Thanks for help.

What if the number has two digits? You will not catch it with that

I know the requirements of the challenge. I was just telling you why the code you have now doesn’t work. You can’t do charAt(3) because right now you will only enter into the switch when num is single digit, so charAt(3) will just return an empty string. Also, as you are right now only testing for single digits, it doesn’t make sense to have it anyway.

var num = 1;
num.toString().charAt(3) // returns ""

Edited the main post, now its working.
I understand what you say, that was just an (bad)example of the whole task. Sorry for that, it was my first post.
Thank you for your time.

Instead of typing out 4 separate switch statements for each digit of the number, I wrote out a separate function with one switch statement that I could call with different characters as arguments for each digit. I thought it might give you an idea of how to do more work with less typing.